Here's a great example of Lua-C integration here and here .
If you just need to export the function to the global namespace, then:
- Declare a function (let's call it
f ) with the signature lua_CFunction . - Call
lua_register(L, "myfunc", f) , where L is the Lua state and the function = f . - Run the lua code. Then
f will be available in the global namespace as myfunc .
If you intend to use a stock interpreter, you may want to create a library. This guy wrote an article for Lua Programming Gems that explains how to do this. Sources are available online.
source share