Proper way to statically link libraries.

  • From: Tim Caswell <tim@xxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 19 Sep 2012 18:03:52 -0500

I'm trying to make a simple binary that statically linked in luajit
and glfw3.  I then want to use ffi to call the glfw3 apis.

My program is very simple:

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

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

    #define GLFW_INCLUDE_GLU
    #include "GL/glfw3.h"

    int main(int argc, char* argv[]) {
      glfwInit();
      printf("%p\n", glfwInit);
      lua_State* L = luaL_newstate();
      luaL_openlibs(L);

      if (argc < 2) {
        fprintf(stderr, "Please enter script to execute\n");
        return 1;
      }
      if (luaL_loadfile(L, argv[1])) {
        fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
        return 1;
      }

      if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
        fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
        return 1;
      }

      lua_close(L);
      return 0;
    }

It compiles just fine.  This is my makefile:

    app: app.o luajit/src/libluajit.a glfw/src/libglfw3.a
      $(CC) -g app.o luajit/src/libluajit.a glfw/src/libglfw3.a -lX11
-lGL -lglut -lGLU -lXrandr -lm -ldl -o app

    app.o: app.c
      $(CC) -g -c app.c -Iglfw/include -Ilibuv/include -Iluajit/src

    luajit/src/luajit.a:
      $(MAKE) -C luajit

    glfw/src/libglfw3.a: glfw/Makefile
      $(MAKE) -C glfw src/libglfw3.a

    glfw/Makefile:
      cmake glfw

Then my lua script is:

    local ffi = require('ffi')

    ffi.cdef[[
      int  glfwInit(void);
    ]]

    ffi.C.glfwInit()

It throws a runtime exception saying "undefined symbol: glfwInit".  I
have no idea what I'm doing wrong.  The symbol is clearly there.  If I
check the binary with the nm utility, it's there marked in a "T"
section.

Can someone shed some light on this issue?

Other related posts: