Search code examples
c++linuxx11glfwvulkan

Linker error on GLFW on linux (x11)


I just installed GLFW3 on Ubuntu and I'm testing the example code from the documentation (http://www.glfw.org/documentation.html) and I got those errors.

I tried multiples thing like adding -ldl, but nothing seems to work.

Here is the error log (GCC):

g++  -o bin/Debug/Game obj/Debug/main.o  -ldl -lglfw3 -lm -lGL -lGLU  
//usr/local/lib/libglfw3.a(vulkan.c.o): In function `_glfwInitVulkan':
vulkan.c:(.text+0x42): undefined reference to `dlopen'
vulkan.c:(.text+0xa9): undefined reference to `dlsym'
//usr/local/lib/libglfw3.a(vulkan.c.o): In function `_glfwTerminateVulkan':
vulkan.c:(.text+0x382): undefined reference to `dlclose'
//usr/local/lib/libglfw3.a(vulkan.c.o): In function `glfwGetInstanceProcAddress':
vulkan.c:(.text+0x72f): undefined reference to `dlsym'
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `translateKeyCode':
x11_init.c:(.text+0x6a): undefined reference to `XkbKeycodeToKeysym'
x11_init.c:(.text+0x14a): undefined reference to `XkbKeycodeToKeysym'
x11_init.c:(.text+0x177): undefined reference to `XGetKeyboardMapping'
x11_init.c:(.text+0x191): undefined reference to `XFree'
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `createKeyTables':
x11_init.c:(.text+0xc3e): undefined reference to `XkbGetMap'
x11_init.c:(.text+0xc61): undefined reference to `XkbGetNames'
x11_init.c:(.text+0x137f): undefined reference to `XkbFreeNames'
x11_init.c:(.text+0x1395): undefined reference to `XkbFreeKeyboard'
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `hasUsableInputMethodStyle':
x11_init.c:(.text+0x14b2): undefined reference to `XGetIMValues'
x11_init.c:(.text+0x150c): undefined reference to `XFree'
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `getSupportedAtom':
x11_init.c:(.text+0x155b): undefined reference to `XInternAtom'
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `detectEWMH':
x11_init.c:(.text+0x15ea): undefined reference to `XInternAtom'
x11_init.c:(.text+0x1610): undefined reference to `XInternAtom'
x11_init.c:(.text+0x1678): undefined reference to `XFree'
x11_init.c:(.text+0x16a1): undefined reference to `XFree'
x11_init.c:(.text+0x16ad): undefined reference to `XFree'
x11_init.c:(.text+0x16be): undefined reference to `XFree'
x11_init.c:(.text+0x16ca): undefined reference to `XFree'
//usr/local/lib/libglfw3.a(x11_init.c.o):x11_init.c:(.text+0x190d): more undefined references to `XFree' follow
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `initExtensions':
x11_init.c:(.text+0x194e): undefined reference to `dlopen'
x11_init.c:(.text+0x1993): undefined reference to `dlsym'
x11_init.c:(.text+0x19c1): undefined reference to `dlsym'
x11_init.c:(.text+0x19ef): undefined reference to `dlsym'
x11_init.c:(.text+0x1a1d): undefined reference to `dlsym'
x11_init.c:(.text+0x1a8b): undefined reference to `dlopen'
x11_init.c:(.text+0x1ad0): undefined reference to `dlsym'
x11_init.c:(.text+0x1afe): undefined reference to `dlsym'
x11_init.c:(.text+0x1b56): undefined reference to `XQueryExtension'
x11_init.c:(.text+0x1bdf): undefined reference to `dlopen'
x11_init.c:(.text+0x1c24): undefined reference to `dlsym'
x11_init.c:(.text+0x1c52): undefined reference to `dlsym'
x11_init.c:(.text+0x1c80): undefined reference to `dlsym'
x11_init.c:(.text+0x1cae): undefined reference to `dlsym'
x11_init.c:(.text+0x1cdc): undefined reference to `dlsym'
//usr/local/lib/libglfw3.a(x11_init.c.o):x11_init.c:(.text+0x1d0a): more undefined references to `dlsym' follow
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `initExtensions':
x11_init.c:(.text+0x214a): undefined reference to `dlopen'
x11_init.c:(.text+0x218f): undefined reference to `dlsym'
x11_init.c:(.text+0x21bd): undefined reference to `dlsym'
x11_init.c:(.text+0x21eb): undefined reference to `dlsym'
x11_init.c:(.text+0x220d): undefined reference to `dlopen'
x11_init.c:(.text+0x2252): undefined reference to `dlsym'
x11_init.c:(.text+0x2280): undefined reference to `dlsym'
x11_init.c:(.text+0x22ae): undefined reference to `dlsym'
x11_init.c:(.text+0x23b4): undefined reference to `XkbQueryExtension'
x11_init.c:(.text+0x23f3): undefined reference to `XkbSetDetectableAutoRepeat'
x11_init.c:(.text+0x2420): undefined reference to `dlopen'

Solution

  • Order of libraries when linking matter. If library A depends on library B, then A must come before B on the command line.

    In your case, since the glfw3 library depends on the dl library, you must switch order so that glfw3 comes before dl on the command line.

    You also must link with the X11 library.

    So the full command line should look something like

    g++ -o bin/Debug/Game obj/Debug/main.o -lglfw3 -ldl -lm -lGL -lGLU -lX11