1

I have source file

#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>

#define BUFSIZE 1024

int main() {
  char line[BUFSIZE];

  lua_State *L = lua_open();
  while (fgets(line, sizeof(line), stdin) != 0) {
    lua_dostring(L, line);
  }
  lua_close(L);
  exit(0);
}

Then I run gcc luac.c, got an error.

Undefined symbols for architecture x86_64:
  "_lua_close", referenced from:
      _main in luac-9bbdf6.o
  "_lua_dostring", referenced from:
      _main in luac-9bbdf6.o
  "_lua_open", referenced from:
      _main in luac-9bbdf6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems like that I have no lua.h in my /usr/local/include folder. But it have.

ls -al /usr/local/include/lua.h 
-rw-r--r--@ 1 root  admin    11K Apr  3 16:23 /usr/local/include/lua.h

And I have

ls -al /usr/local/lib/liblua.a
-rw-r--r--  1 root  admin  150448 Apr  3 16:23 /usr/local/lib/liblua.a

The gcc search path

gcc -Xlinker -v 
(#)PROGRAM:ld  PROJECT:ld64-305
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
    /usr/lib
    /usr/local/lib
Framework search paths:
    /Library/Frameworks/
    /System/Library/Frameworks/
...

Why gcc says error, can you tell me.


Updated:

The problem is gcc default doesn't add lua lib?

I use gcc luac.c -llua -llualib, works.

Community
  • 1
  • 1
user3875388
  • 541
  • 1
  • 6
  • 19
  • 2
    You don't actually *link* with the Lua library? Also see [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Some programmer dude Apr 03 '18 at 08:47
  • I doesn't know, I will check the link. – user3875388 Apr 03 '18 at 09:04
  • 1
    "It seems like that I have no lua.h..." The compiler doesn't complain about missing file here. So obviously you have the header in your include path. – Gerhardh Apr 03 '18 at 09:10
  • Yes, it's the link can't find `liblua.a`, I add the path to `gcc`, The problem solved, thx~ – user3875388 Apr 03 '18 at 09:14
  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Bartek Banachewicz Apr 03 '18 at 09:18
  • `export LD_LIBRARY_PATH=/usr/local/lib` and compile, no need to specify manually – ntshetty Apr 03 '18 at 09:23
  • Yes, you are right. – user3875388 Apr 03 '18 at 09:29

0 Answers0