Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call C++ 's functions and libraries by Lua

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how Lua calls C++ 's functions and libraries". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how Lua calls C++ 's functions and libraries".

Article catalogue

The first way

The second way

The first way

Create a new DLL empty win32 project and create a new test.cpp file, as follows

/ * Lua calls Candlespace + function / library (dynamic link mode) * / # includeusing namespace std;#includestatic int math_abs (lua_State * L) {lua_pushnumber (L, abs ((int) luaL_checknumber (L, 1)); return 1;} static int math_cos (lua_State * L) {lua_pushnumber (L, cos ((double) luaL_checknumber (L, 1)); return 1 } static int math_sin (lua_State * L) {lua_pushnumber (L, sin ((double) luaL_checknumber (L, 1)); return 1;} static const luaL_reg mathlib [] = {{"abs", math_abs}, {"cos", math_cos}, {"sin", math_sin}, {NULL, NULL}} / / dll is exported through the function luaI_openlib, and then lua uses package.loadlib to import the library function extern "C" _ declspec (dllexport) int LuaAPIDLL (lua_State* L) / /. The name of this function is consistent with the library name {luaI_openlib (L, "DY_MATH", mathlib,0); return 1;}

Lualoadlib.lua file

-- region lualoadlib.lualocal libpath= ". /.. / Debug/LuaAPIDLL.dll" local loadlibfunc=package.loadlib (libpath, "LuaAPIDLL") loadlibfunc () function COS (a) print ("called COS in lua script") return DY_MATH.cos (a) endfunction SIN (a) print ("called SIN in lua script") return DY_MATH.sin (a) endprint (COS (60mm 3.1415926)) print (SIN (30) os.execute ("pause")-- double-click this file directly The cmd window won't disappear-- endregion

Double-click the lualoadlib.lua file directly, and the result is as follows

The second way

Test.cpp partial code

# includeusing namespace std;#includestatic int testFunc (lua_State* L) {printf ("http://www.jellthink.com\n"); lua_pushnumber (L, 10); return 1;} static const struct luaL_Reg myLib [] = {{" test ", testFunc}, {NULL, NULL}}; extern" C "_ declspec (dllexport) int luaopen_LuaAPIDLL (lua_State* L) {luaL_register (L," testDll ", myLib); return 1 } / * LUA calls external DLL,DLL contains library functions and throw functions, when you are not using package.loadlib to use external DLL, but using another way require, then you need to pay attention to some rules: the throwing function prototype in DLL must be: [extern "C"] _ declspec (dllexport) int luaopen_XXX (LuaState* L), and XXX is the file name of DLL. [] is an optional symbol, and the use of package.loadlib does not require these rules, but for the sake of compatibility, that is, DLL can be used in both ways, or the throwing function can be named according to the rule. , /

The test.lua file should be in the same directory as the LuaAPIDLL.dll file, as follows

Require "LuaAPIDLL" local a = testDll.test () print (a) os.execute ("pause")

Double-click, test.lua file output

Thank you for reading, the above is the content of "how Lua calls C++ 's functions and libraries". After the study of this article, I believe you have a deeper understanding of how Lua calls C++ 's functions and libraries, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report