简体   繁体   中英

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_openlibs(L);
luaL_dofile(L, "test.lua");
lua_close(L);

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?

Edit:

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.

Example of a hello world in LUA + C++

main.cpp

#include <iostream>

#ifdef __cplusplus
  extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
  }
#endif //__cplusplus

int main(int argc, char **argv) {
    std::cout << "LUA is saying: ";

    lua_State* L;
    L = luaL_newstate();
    lua_pushstring(L, "Anna");
    lua_setglobal(L, "name");
    luaL_openlibs(L);
    if(luaL_dofile(L, "hello.lua")) {
        std::cout << "Final:" << lua_tostring(L, -1) << "\n";
    }
    lua_close(L);
}

hello.lua

print("Hello World!")
print("Hello", name)

Makefile

hello: main.cpp
    g++ -I/usr/include/lua5.3/ main.cpp -llua5.3 -o hello

The example is creating a new lua_State which is the main context that will be used by the lua engine to execute the code. As a next step, we need to open the libraries and, if required, add additional libraries that will be used by the lua .

Before we start with the execution, we can set the variables in the global context that will be accessed by the lua code by pushing the values on the stack and calling lua_setglobal() to set the global variable.

Finally, we are using the helper function luaL_dofile() which will execute the file and returns false (0) in case there are no errors, or true (1) in case there are errors. In case of error, the last value of the stack in the state will be the error description.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM