Using lua5.3
DllMain.cpp
1 //生成的dll 是 lua_add53.dll 2 //luaopen_lua_add 3 extern "C" { 4 #include "F:/lua_src/lua-5.3.5_Win64_vc15_lib/include/lua.h" 5 #include "F:/lua_src/lua-5.3.5_Win64_vc15_lib/include/lualib.h" 6 #include "F:/lua_src/lua-5.3.5_Win64_vc15_lib/include/lauxlib.h" 7 }; 8 #pragma comment(lib, "F:/lua_src/lua-5.3.5_Win64_vc15_lib/lua53.lib") 9 10 #include <iostream> 11 using namespace std; 12 13 extern "C" int ShowMsg(lua_State* luaEnv) { 14 cout << "Hello world from clibs!" << endl; 15 return 0; // 返回值个数为0个. 16 } 17 18 extern "C" int sub2(lua_State* L) 19 { 20 double op1 = luaL_checknumber(L, 1); 21 double op2 = luaL_checknumber(L, 2); 22 is int TEMP = OP1 - OP2; 23 is lua_pushnumber (L, TEMP); 24 return . 1 ; 25 } 26 is 27 // Part One: To export a list of functions 28 static luaL_Reg luaLibs [] = { 29 { " ShowMsg " , } ShowMsg, 30 { " SUB2 " , SUB2}, 31 is {NULL, NULL} 32 }; 33 is 34 is // Part TWO: DLL entry function, Lua calls associated with this DLL. 35 extern " C "__declspec (dllexport) 36 int luaopen_lua_add (lua_State * luaEnv) { // WinFeature is modole name, the name of the future require 37 // lua_register (luaEnv, "ShowMsg", ShowMsg); // critical line on luaState up for good this lib 38 is // lua_register (luaEnv, "SUB2", SUB2); // key line, the good registration on luaState lib 39 lua_newtable (luaEnv); 40 luaL_setfuncs (luaEnv, luaLibs, 0 ); 41 is return . 1 ; 42 is }
After compilation is lua_add53.dll
take
1 local mytest = require "lua_add" 2 mytest.ShowMsg(); 3 print(mytest.sub2(1,2));