3

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.

Community
  • 1
  • 1
Letokteren
  • 749
  • 1
  • 15
  • 28
  • 2
    I don't know how to do that in C::B, but you have to link the lua libraries. Alternatively, you can also just include the source files to your project, but thats not the "right" way to go. Just link the libraries like any other library too. – pschulz Jul 14 '16 at 12:20
  • @pschulz I thought Lua was designed so you could just add the source files. – user253751 Jul 14 '16 at 12:28
  • @pschulz Is there a general explanation how to link libraries? Every time I search for this I get vague explanations on forums, but never a well written step by step tutorial. It really gives me the feeling that I've missed something very trivial when I learned the language. When I set up SDL before, I was lucky since Lazyfoo explains everything in great detail. At this point, I have all my .h files in the include library, and all the .c files next to the .cpp file, but I still receive the same error. – Letokteren Jul 14 '16 at 12:38
  • 1
    @Letokteren maybe read these resources: http://tldp.org/HOWTO/Program-Library-HOWTO/index.html , https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html – Jesper Juhl Jul 14 '16 at 14:58
  • 1
    I think the guide mentioned by @Jesper Juhl is good to know, but here the TL;DR answer, since it is a bit much for a beginner. Two ways: command line (i'm mentioning this since it helps to understand how things work): you compile via `gcc main.cpp -I /path/to/include -l library`, where the file for library is something like liblibrary.so. For code blocks, see this answer: http://stackoverflow.com/questions/5862757/how-do-i-link-to-a-library-with-codeblocks/5881751#5881751 – pschulz Jul 14 '16 at 15:44
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Colonel Thirty Two Jul 14 '16 at 21:10

2 Answers2

1

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.

Letokteren
  • 749
  • 1
  • 15
  • 28
0

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.

jordanvrtanoski
  • 5,104
  • 1
  • 20
  • 29