Using Lua in C++

I'm trying to use Lua in C++, but I can not compile my code. I'm using the latest version of Lua, which is 5.3 at the moment. My IDE is Code::Blocks. So far I was following guides like these: https://eliasdaler.wordpress.com/2013/10/11/lua_cpp_binder/ http://gamedevgeek.com/tutorials/getting-started-with-lua/

However they don't explain much about how to set up Lua in C::B. I've downloaded both the binary zip and the source from Lua's website. I'm not sure where to put the files from the src folder. So far I've put the lauxlib.h, the lua.h, the luaconf.h and the lualib.h into the include directory, and used the following code in the main.cpp:

extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"

So far I'm just trying to run the following small snippet:

lua_State* L;
L = luaL_newstate();
luaL_dofile(L, "test.lua");

Yet I always get an error at the first line. The error I'm getting at the moment states that the reference is undefined to 'luaL_newstate'.

Maybe I should put some files in the lib directory from the source? Or is there anything I have to add to the 'Other linker options' in the 'Project build options' menu?


In the mean time I've found this question: Lua 5.3 undefined references

It seems I have to put the -llua to the 'Opther linker options', but there are no .a, .so or .lib files included in the packages at lua.org.


  • I could finally make the code mentioned in the question to run. Here's what I did.

    1. Instead of downloading the source from Lua's site, I downloaded the latest LuaDist. The site is also accessible via the Lua.org's download page under the Binaries category.

    2. In that zip, there's the usual include and lib folders.

    3. I've copied both to the appropriate folders, and set the path for compiler and the linker under the Build options menu.

    4. Then, under the Linker settings tab, I've added the liblua.dll.a file, which can be found in the lib directory. For this last two step, you can find additional help at the SFML setup page.

    5. For the final step, I've placed the liblua.dll file next to the compiled executable.