lua call c/c++ function

this article is tenchnique reminder

key point:

1. c/c++ DLL

2. below mylib as exported name required and called from lua script, should be same

extern "C"

{

#include "lua.h"

#include "lualib.h"

#include "lauxlib.h"

 

int lhsadd(lua_State *L)

{

return 1;
}

 

int lhssubs(lua_State *L)

{

return 1;
}

static luaL_Reg libs[] = {{"lhsadd",lhsadd},{"lhssubs",lhssubs},{NULL,NULL}};

 

 

__declspec(dllexport) in luaopen_mylib(lua_State *L)

{

const char* libname = "mylib2";

luaL_register(L,libname,libs);

return 1;

}

 

}

 

 

.lua testing script

require "mylib"

print(mylib2.lhsadd(1.1,2.2))

print(mylib2.lhsadd(2.2,1.1))

 

 

 

 

 

 

 

 

你可能感兴趣的:(lua)