5

Hey everyone I found this code that embeds Lua in C and I cannot figure out how to get GCC to compile it. I have Lua installed, but how do I link the Lua libraries?

Here is the code I found:

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

            /* lua interpreter */
            lua_State* l;

            int main () {
            int dofile;

            /* initialize lua */
            l = lua_open();

            /* load lua libraries */
            luaL_openlibs(l);

            /* run the hello.lua script */
            dofile = luaL_dofile(l, "hello.lua");

            if (dofile == 0) {
            /* call foo */
            lua_getglobal(l,"foo");
            lua_call(l,0,0);
            }
            else {
            printf("Error, unable to run hello.lua\n");
            }

            /* cleanup Lua */
            lua_close(l);

            return 0;
            }

How do I get this to compile?

I am trying this command to compile

gcc -o embed_hello -L/users/etrosclair/Downloads/lua-5.1.4 -I/users/etrosclair/Downloads/lua-5.1.4 luaTest.c

Here is the error:

Undefined symbols for architecture x86_64:
  "_luaL_newstate", referenced from:
      _main in ccF0995Q.o
  "_luaL_openlibs", referenced from:
      _main in ccF0995Q.o
  "_luaL_loadfile", referenced from:
      _main in ccF0995Q.o
  "_lua_pcall", referenced from:
      _main in ccF0995Q.o
  "_lua_getfield", referenced from:
      _main in ccF0995Q.o
  "_lua_call", referenced from:
      _main in ccF0995Q.o
  "_lua_close", referenced from:
      _main in ccF0995Q.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

All the lua libraries and headers are in the lua-5.1.4 folder the .o files are also in there too.

Thanks

Thanks

1
  • 4
    probably something like -llua on the command line. Why don't you show us some errors? Commented Nov 6, 2011 at 0:57

1 Answer 1

8

Depends if you want it statically or dynamically compiled.

For static, add -llua (or lua5.1 or lua51; depending on your setup)

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

2 Comments

It also works for shared libraries, so OP should try adding -llua, -llua5.1 and -llua-5.1 to the command line.
if available on your system, you could use gcc -o hello hello.o $(pkg-config --libs lua)

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.