LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Undefined reference error when using Lua API (https://www.linuxquestions.org/questions/programming-9/undefined-reference-error-when-using-lua-api-892782/)

MTK358 07-20-2011 02:10 PM

Undefined reference error when using Lua API
 
I wrote a small test program, and this is what happens when I try to compile it:

Code:

$ g++ -llua main.cpp
/tmp/ccHJ8HR3.o: In function `main':
main.cpp:(.text+0xc): undefined reference to `luaL_newstate()'
main.cpp:(.text+0x1c): undefined reference to `luaL_openlibs(lua_State*)'
main.cpp:(.text+0x28): undefined reference to `lua_close(lua_State*)'
collect2: ld returned 1 exit status\

I do have Lua installed, and I have liblua.a and liblua.so in /usr/lib.

knudfl 07-20-2011 03:34 PM

Arch, 64bits : ls /usr/lib/liblua*
/usr/lib/liblua.a /usr/lib/liblua.so@ /usr/lib/liblua.so.5.1*

The objects luaL_newstate luaL_openlibs lua_close are present :

readelf -s /usr/lib/liblua.so | grep luaL_newstate
134: 00000000000194f0 43 FUNC GLOBAL DEFAULT 11 luaL_newstate
554: 00000000000194f0 43 FUNC GLOBAL DEFAULT 11 luaL_newstate

etc. etc.

Are you missing 'liblua.so.5.1' ? Or is it not an "ELF 64-bit LSB shared object, x86-64" ?
( The file command and 'ls -l /usr/lib/liblua.so' will show, if it is a valid link.)

..

MTK358 07-20-2011 03:45 PM

Code:

$ ls -l /usr/lib/liblua*
-rw-r--r-- 1 root root 345020 Jul 28  2010 /usr/lib/liblua.a
lrwxrwxrwx 1 root root    13 Jul 28  2010 /usr/lib/liblua.so -> liblua.so.5.1
-rwxr-xr-x 1 root root 183992 Jul 28  2010 /usr/lib/liblua.so.5.1

EDIT: Why does the "liblua.so" symlink have 777 permissions?

markush 07-20-2011 04:02 PM

Quote:

Originally Posted by MTK358 (Post 4420513)
EDIT: Why does the "liblua.so" symlink have 777 permissions?

Symlinks have always 777 permissions.

Markus

knudfl 07-20-2011 04:04 PM

777 for <lib>.so links :

Never thought about it. Seems to be the same for other libraries too.

ta0kira 07-20-2011 04:35 PM

You need to place the lib after the .cpp file so it gets searched after the linker knows what dependencies the .cpp has.
Kevin Barry

MTK358 07-20-2011 05:14 PM

Quote:

Originally Posted by ta0kira (Post 4420556)
You need to place the lib after the .cpp file so it gets searched after the linker knows what dependencies the .cpp has.

If you mean this:

Code:

g++ main.cpp -llua
Then it didn't help.

ta0kira 07-20-2011 05:31 PM

Quote:

Originally Posted by MTK358 (Post 4420586)
If you mean this:

Code:

g++ main.cpp -llua
Then it didn't help.

That's what i meant.

Try wrapping the #include with extern "C" { } in case g++ is mangling the library symbols from the header.
Kevin Barry

MTK358 07-20-2011 07:14 PM

Quote:

Originally Posted by ta0kira (Post 4420602)
Try wrapping the #include with extern "C" { } in case g++ is mangling the library symbols from the header.

That works!

I'm marking the thread as solved.


All times are GMT -5. The time now is 04:20 AM.