2

I try to Compile this Simple Lua Tutorial Program:

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

    int main (void) {
      char buff[256];
      int error;
      lua_State *L = lua_open();   /* opens Lua */
      luaopen_base(L);             /* opens the basic library */
      luaopen_table(L);            /* opens the table library */
      luaopen_io(L);               /* opens the I/O library */
      luaopen_string(L);           /* opens the string lib. */
      luaopen_math(L);             /* opens the math lib. */

      while (fgets(buff, sizeof(buff), stdin) != NULL) {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
                lua_pcall(L, 0, 0, 0);
        if (error) {
          fprintf(stderr, "%s", lua_tostring(L, -1));
          lua_pop(L, 1);  /* pop error message from the stack */
        }
      }

      lua_close(L);
      return 0;
    }

With the Following Command:

gcc -I/usr/include/lua50 -L/usr/lib/liblua50.a -llua50 luainterpret.c

So the Headers are Linked and Library Binary should also be Linked right?

However i get the following undefined References:

/tmp/ccA3kOUt.o: In function `main':
luainterpret.c:(.text+0x1b): undefined reference to `lua_open'
luainterpret.c:(.text+0x31): undefined reference to `luaopen_base'
luainterpret.c:(.text+0x40): undefined reference to `luaopen_table'
luainterpret.c:(.text+0x4f): undefined reference to `luaopen_io'
luainterpret.c:(.text+0x5e): undefined reference to `luaopen_string'
luainterpret.c:(.text+0x6d): undefined reference to `luaopen_math'
luainterpret.c:(.text+0xa1): undefined reference to `luaL_loadbuffer'
luainterpret.c:(.text+0xc3): undefined reference to `lua_pcall'
luainterpret.c:(.text+0xf6): undefined reference to `lua_tostring'
luainterpret.c:(.text+0x11f): undefined reference to `lua_settop'
luainterpret.c:(.text+0x152): undefined reference to `lua_close'
collect2: error: ld returned 1 exit status

I checked the /usr/lib/liblua50.a file with nm and the Functions above are indeed there! Why is gcc then not able to find said Functions? Can someone tell me what im doing wrong?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

1 Answers1

2

Instead of putting the library before the source file (which makes use of the functions present in the library), try putting it afterwards, like

gcc -I/usr/include/lua50 -L/usr/lib/liblua50.a  luainterpret.c -llua50

From the online gcc manual

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Yeah that was it! with the following command the Compilation completes: ```gcc -I/usr/include/lua50 -L/usr/lib/ luainterpret.c -llua50 -llualib50``` Thanks for the Help :) –  Mar 29 '19 at 11:39