2

In a nutshell: compiling and linking c++ program that embeds lua using command g++ -o clua clua.cpp -Wall -Iinclude -L liblua5.1.a liblua5.1.so -ldl, getting liblua5.1.so: undefined reference to 'dlopen', among others.

I've been unsuccessfully trying to embed any scripting language in my c++ game for a few weeks now, going from V8 through Python and Squirrel. Lua is marketed as "easy to setup, small", etc, so I figured I'd go with that.

I've gotten the precompiled lua binaries from http://sourceforge.net/projects/luabinaries/files/5.1.5/Linux%20Libraries/ (I used lua-5.1.5_Linux26g4_lib.tar.gz for my 32bit ubuntu 13.something) and have a simple example program:

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

int main()
{
    lua_State *L = lua_open();

    // load the libs
    luaL_openlibs(L);

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

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

    lua_close(L);

    return 0;
}

that is saved as "clua.cpp" and placed in the extracted folder (so that it is in the same directory as "liblua5.1.a" and "liblua5.1.so" and the lua header files are in a folder called "include"). Now, I'm trying to compile the program with the console, using g++, this is the exact command:

g++ -o clua clua.cpp -c -Wall -Iinclude

And it runs flawlessly, produces no errors, and creates a binary file "clua" as expected. When I try to link however:

g++ -o clua clua.cpp -Wall -Iinclude -L liblua5.1.a liblua5.1.so -ldl

I get:

liblua5.1.so: undefined reference to `dlopen'
liblua5.1.so: undefined reference to `dlclose'
liblua5.1.so: undefined reference to `dlerror'
liblua5.1.so: undefined reference to `dlsym'
collect2: error: ld returned 1 exit status

I have read lots of responses to people having similar questions, options presented are:

  • adding "-ldl", no effect there
  • making sure "-ldl" is last, did that too
  • https://projects.coin-or.org/Ipopt/ticket/230 said adding LDFLAGS="-Wl,--no-as-needed" or --enable-dependency-linking should do the trick, now, I was not sure exactly how my command was supposed to look, whether to put

    g++ -o clua LDFLAGS="-Wl,--no-as-needed" clua.cpp -Wall -Iinclude -L liblua5.1.a liblua5.1.so -ldl

or

g++ -o clua -Wl --no-as-needed clua.cpp -Wall -Iinclude -L liblua5.1.a liblua5.1.so -ldl

,

g++ --enable-dependency-linking -o clua clua.cpp -Wall -Iinclude -L liblua5.1.a liblua5.1.so -ldl 

or

g++ -o clua clua.cpp -Wall -Iinclude -L liblua5.1.a liblua5.1.so -ldl --enable-dependency-linking

but all those gave errors like:

cc1plus: error: unknown pass dependency-linking specified in -fenable
  • https://projects.coin-or.org/Ipopt/ticket/229 said: "Another workaround would be to add the configure flag --disable-pthread-mumps", I did that:

    g++ -o clua --disable-pthread-mumps clua.cpp -Wall -Iinclude -L liblua5.1.a liblua5.1.so -ldl

and got

cc1plus: error: unknown pass pthread-mumps specified in -fdisable

I was running this all from the directory where clua.cpp, liblua5.1.a and liblua5.1.so was, and there was a folder in there named "include" containing headers. I'm running this on 32bit Ubuntu 13.(10, I think).

Now, what can I do to link Lua properly?

4
  • remember, select isn't broken, You might want to see, which kinds of lua a aptitude-distributable on your linux and use that, and use a meta-build generator to avoid compile flags errors. See an example here->premake4.lua. The problem might be as simple, as that you are just writing wrong flags for gcc May 23, 2014 at 10:35
  • select isn't broken - nice article, tho I knew most of these things before. Well yes, you're probably right, I'm probably doing something wrong - it's just that I've personally ran out of other things to try. That's why I'm here. I'll go into the example you gave, but if you have the time, could you elaborate on what the problem with my flags might be? As you saw, I tried some things in that area (--enable-dependency-linking, etc.) but perhaps I am putting those in the wrong place in the command?
    – Ludwik
    May 23, 2014 at 12:27
  • It may be simpler just to include the source of Lua into your project and build it with -DLUA_USE_LINUX. If you don't need to dynamically load C libraries for Lua, you can disable it in luaconf.h and avoid -ldl.
    – lhf
    May 23, 2014 at 14:35
  • usage of libdl can be disabled? Man, that might solve it all. Will try that first.
    – Ludwik
    May 23, 2014 at 16:33

1 Answer 1

2

-L liblua5.1.a liblua5.1.so looks wrong → link options-llua5.1?

Then, there are two different lua libraries on Ubuntu/Debian, as far as I remember, one of which doesn't allow dynamic library loading, hence -llua5.1-c++ and to get it: sudo apt-get install lua5.1 liblua5.1-dev

My advice: simplify, use a meta-make generator if you're struggling with GNU Make

3
  • This looks promising. I will check it as soon as I get home. As a comment tho - I tried -llua5.1 first, and I think it could not find it. But I'm not sure on that, will try again.
    – Ludwik
    May 23, 2014 at 13:06
  • Hah, don't bother, it sure is installed ;). Works from the command line, I can type "lua" and use it interactively.
    – Ludwik
    May 23, 2014 at 13:43
  • 1
    Doing sudo apt-get install lua5.1 liblua5.1-dev worked! Man, I can't say how thankful I am; I've been unsuccesful in doing this for weeks and it has really stalled my project. And you were right, "select" wasn't broken. So yeah, thank you, really.
    – Ludwik
    May 23, 2014 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.