2

I made a C program and wrote the following two function in that

1. int c_add(lua_State* L)  
   {...some code here...  
   }  
2. int main(int argc, char* argv)  
   {...some code here...  
   lua_register(L, "c_add", c_add);  
   }  

And compiled it by following command successfully.

gcc -o test.exe test.c -I /home/ec2-user/install/lua-5.1.5/src -llua-5.1

But the following error showed after using a lua program to call it.

lua: func2.lua:2: attempt to call global 'c_add' (a nil value)  

How to solve this problem?`

4
  • 1
    What is the file func2.lua and how are you running it? Commented Dec 20, 2018 at 6:09
  • @cyclaminist thank you for reply. The code is here. -- func2.lua print(c_add(3,4)) Commented Dec 20, 2018 at 6:27
  • @cyclaminist And I run it by lua func2.lua. Commented Dec 20, 2018 at 6:34
  • 2
    lua.exe is not the test.exe. You have c_add function defined by the code in test.exe, so you must run func2.lua from within the test.exe Commented Dec 20, 2018 at 9:40

1 Answer 1

3

You need to compile the code as a shared library to access the C functions from an external Lua instance. You don't need the main function and you pass the -shared flag to gcc. Then you need to let Lua know what to do when require is called by implementing the following function:

//             same name as the so/dll
//                      v
LUALIB_API int luaopen_test(lua_State *L) {
    lua_register(L, "c_add", c_add);
    return 0;
}

This creates a single, global function. It's better to register a luaL_Reg array of functions with luaL_register:

static const luaL_Reg function_list[] = {
    {"c_add",   c_add},
    {NULL, NULL}
};

LUALIB_API int luaopen_test(lua_State *L) {
    luaL_register(L, "test", function_list);
    return 1;
}

So that a table of functions is returned by require:

local my_c_functions = require("test")
print(my_c_functions.c_add(42, 42))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I now have a deeper understanding of this. But a new error appeared when I using the code above. lua: error loading module 'test' from file '/usr/local/lib/lua/5.3/test.so': /usr/local/lib/lua/5.3/test.so: undefined symbol: luaL_register Does it means I have to include come library in C program?
luaL_register is from Lua 5.1. In Lua 5.3, luaL_register is not defined unless you have compiled Lua with LUA_COMPAT_MODULE defined, so if you are compiling your library for Lua 5.3, it would be better to replace luaL_register(L, "test", function_list) with luaL_newlib(L, function_list) (and to make sure you are using the Lua 5.3 versions of the Lua headers).
@cyclaminist Thank you for detailed explanation. I've replaced the function by following your advice, but the next error appeared. lua: error loading module 'test' from file '/usr/local/lib/lua/5.3/test.so': /usr/local/lib/lua/5.3/test.so: undefined symbol: luaopen_test But there is no "luaopen_test" in my source code.
As PaulR explained above, your library needs a function for the Lua VM to call when you require the library from Lua. In this case, the function needs to be named luaopen_test. See the documentation for package.searchers for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.