3

test.c :

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main(){

  lua_State *L = luaL_newState();  /* opens Lua */
  luaL_openlibs(L);   /* opens the standard libraries */

  luaL_dofile(L,"test.lua"); /* runs Lua scrip */

  lua_close(L);
  return 0;

}

build:

gcc -o test test.c -I/usr/local/Cellar/lua/5.1.5/include

then i got error:

test.c:7:18: warning: implicit declaration of function 'luaL_newState' is
      invalid in C99 [-Wimplicit-function-declaration]
  lua_State *L = luaL_newState();  /* opens Lua */
                 ^
test.c:7:14: warning: incompatible integer to pointer conversion initializing
      'lua_State *' (aka 'struct lua_State *') with an expression of type 'int'
      [-Wint-conversion]
  lua_State *L = luaL_newState();  /* opens Lua */
             ^   ~~~~~~~~~~~~~~~
2 warnings generated.
Undefined symbols for architecture x86_64:
  "_luaL_loadfile", referenced from:
      _main in test-dxPwkn.o
  "_luaL_newState", referenced from:
      _main in test-dxPwkn.o
  "_luaL_openlibs", referenced from:
      _main in test-dxPwkn.o
  "_lua_close", referenced from:
      _main in test-dxPwkn.o
  "_lua_pcall", referenced from:
      _main in test-dxPwkn.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I installed Lua via Homebrew:

ls /usr/local/Cellar/lua/5.1.5/include
lauxlib.h lua.h     lua.hpp   luaconf.h lualib.h

Anyone knows how to solve this error? Thanks.

1
  • 3
    You need to add either the import library or the lua*.so shared object when building your example. The undefined refs comes from the linker not know where those lua C functions are. I'm not sure where this would be on your linux setup. Maybe try a which lua51.so or look in one of your bin directories. Also move your -I parameter before test.c and that should fix the warnings. Commented Dec 8, 2013 at 6:36

2 Answers 2

5

It's luaL_newstate, not luaL_newState.

You also need -llua -lm at the end of the command line.

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

Comments

0

when u make it, u should ld the .so file of lua which may be in /usr/local/lib, find out it, and use the -L to add the library path, use -l to link the library.

finally, after the make step, if the program cannot run, u should also use ln -s to add a symbol link which is related to the .so in /usr/lib.

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.