2

I am attempting to embed lua code in C++, and I'm getting a strange compiler error. Here's my code:

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

int main() {
    lua_State *luaVM = luaL_newstate();
    if (luaVM == NULL) {
        printf("Error initializing lua!\n");
        return -1;
    }

    luaL_openlibs(luaVM);

    luaL_dofile(luaVM, "test.lua");

    lua_close(luaVM);

    return 0;
}

compiled with:

g++ -Wall -o embed -llua embed.cpp

and the error is:

g++ -Wall -o embed -llua embed.cpp
/tmp/ccMGuzal.o: In function `main':
embed.cpp:(.text+0x47): undefined reference to `luaL_loadfilex'
embed.cpp:(.text+0x72): undefined reference to `lua_pcallk'
collect2: error: ld returned 1 exit status

I am not calling luaL_loadfilex or lua_pcallk from my code, which lends itself to the assumption that the problem is not in my code, but in lua itself. does anyone have any ideas here?

UPDATE

Here is my version info:

$ lua -v
Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
ewok
  • 20,148
  • 51
  • 149
  • 254

4 Answers4

2

Pre 5.1 answer: According to this website, you need to add -llualib after -llua if you include lauxlib.h and lualib.h:

g++ -Wall -o embed embed.cpp -llua -llualib

Update

Silly me, you should always link files/libs in the order they use the other. If A uses B, you should mention A before B.

In your case, since embed.cpp uses lua, then you should write:

g++ -Wall -o embed embed.cpp -llua
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • according to [this answer](http://stackoverflow.com/questions/2441513/lua-initializing), there is no `lualib` in lua >5.1. when I add `-llualib`, i get a new error, backing this up: `/usr/bin/ld: cannot find -llualib` – ewok Jul 31 '12 at 14:40
  • @ewok, what is your lua version? – Shahbaz Jul 31 '12 at 14:58
  • @Shabaz, I have moved the links, but to no avail. – ewok Jul 31 '12 at 15:12
  • @ewok, do you get the exact same error?! Try `nm` on the lib file of lua and check if those symbols exist in it. – Shahbaz Jul 31 '12 at 15:18
  • @ewok, did you `nm` on lua's lib file? – Shahbaz Jul 31 '12 at 15:31
  • If you are in linux, find where you have installed/compiled lua, there should be a file named `liblua.a` or something like that. In that directory, run `nm liblua.a`, it will list all the symbols existing in that file. If it's not there, we have to find which library has defined them. If they are there, we have to figure out why `g++` doesn't find them. – Shahbaz Jul 31 '12 at 16:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14705/discussion-between-shahbaz-and-ewok) – Shahbaz Jul 31 '12 at 16:23
  • Did you install lua from somewhere or did you build from the source? – Shahbaz Jul 31 '12 at 16:24
2

In in lua 5.2.1 luaL_dofile is a macro that is declared like so:

#define luaL_dofile(L, fn) \
    (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

in your version of lua it might very well be implemented with luaL_loadfilex and lua_pcallk, and you get undefined references like @Shahbaz said.

yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
2

Ultimately the problem was that the libraries are named differently by version. When installed from the repository, the libraries are called liblua5.x and liblualib5.x, and therefore the following command was required:

g++ -Wall -o embed embed.cpp -llua5.2 -llualib5.2
ewok
  • 20,148
  • 51
  • 149
  • 254
0

You can use:

cc embed.cpp -o embed -llua -L../lua -I../lua -lm -ldl
Zoe
  • 27,060
  • 21
  • 118
  • 148