hi
i am having a problem integrating Lua into C++. I found a discussion about it here but it didn't help me. i have installed lua and i can see it in /usr/include and /usr/lib and /usr/bin
i created the same code as the previous descussion

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

int main()
{
    int s=0;

    lua_State *L = lua_open();

    // load the libs
    luaL_openlibs(L);

    //run a Lua scrip here
    luaL_dofile(L,"test.lua");

    printf("\nI am done with Lua in C++.\n");

    lua_close(L);

    return 0;
}

the compile command that i use
gcc -llua lpeg_test.cpp

i get

/tmp/ccsSzCTD.o: In function `main':
lpeg_test.cpp:(.text+0x10): undefined reference to `luaL_newstate()'
lpeg_test.cpp:(.text+0x1d): undefined reference to `luaL_openlibs(lua_State*)'
lpeg_test.cpp:(.text+0x2b): undefined reference to `luaL_loadfile(lua_State*, char const*)'
lpeg_test.cpp:(.text+0x47): undefined reference to `lua_pcall(lua_State*, int, int, int)'
lpeg_test.cpp:(.text+0x5a): undefined reference to `lua_close(lua_State*)'
/tmp/ccsSzCTD.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

can someone tell me what am i doing wrong ??

StuXYZ commented: Well written first post. +3

If you are going to use a C library in C++, you need to compile the headers in C
style. That is done like this:

#include <iostream>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

int main()
{
    int s=0;
    lua_State *L = lua_open();
    // load the libs
    luaL_openlibs(L);
    //run a Lua scrip here
    luaL_dofile(L,"test.lua");
    lua_close(L);
    return 0;
}

To compile and built it use: g++ test.cpp -llua You may need to add a -I flag to point to the lua include directories and -L flag to point to your liblua.so library. [In your case, your lua files are in /usr/include and /usr/lib so it is highly unlikely that you will need the -I and -L flags]

the compiler dosent seem to find the lua lib. i compiled and installed the lua again and verified that its in /use/lib. but i still can't seem to compile my application.
???

Can you post the compiler error, if you use -llua to link to the library then you need
to have the liblua.so available. Are youg getting the same error message as before?

Also you can't compile this gcc -llua code.cpp, it has to be g++ -llua code.cpp because you will miss other dependencies.

While you are checking things what version of liblua.so do you have, i.e what does liblua.so point to [it will be a symbolic link]

Finally, it is possible to be wrongly linking against different version to the version that is being included. If you have that possibility, then it can be tested by doing this: g++ code.cpp /usr/lib64/liblua.so were you have explicitly said which file you are going to link against. [note there is no -l in this method as you are directly linking].
You can find the liblua.so with the locate command.

If the problem is that you have two version of the liblua.so then you need to either remove one, or re-arrange your ld.so.conf file (normally in /etc/ld.so.conf) so that you get the correct version first, or use an explicit -L path e.g. -L/usr/lib64

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.