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
3
  • See also: embedding lua code in c Commented Jul 31, 2012 at 17:35
  • @MartinSchröder note that that also was my question. I decided that, having figured out what was causing that problem (lualib doesnt exist), it was better to ask this as a new question that keep editing that one. Commented Jul 31, 2012 at 17:43
  • Ah. Sorry, I didn't notice that. Commented Jul 31, 2012 at 21:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

8 Comments

according to this answer, 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, what is your lua version?
@Shabaz, I have moved the links, but to no avail.
@ewok, do you get the exact same error?! Try nm on the lib file of lua and check if those symbols exist in it.
@ewok, did you nm on lua's lib file?
|
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

Comments

0

You can use:

cc embed.cpp -o embed -llua -L../lua -I../lua -lm -ldl

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.