Search code examples
c++makefilecompiler-errorsg++glfw

Can't link GLFW library with it's header file on Ubuntu 18.04


I've installed the libglfw3-dev:amd64 package on Ubuntu using the standard sudo apt get etc. My following compiling line is:

g++ -o output -IL/usr/lib/x86_64-linux-gnu -lglfw driver.o 

My current c++ file is:

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;
     if (!glfwInit())
        return -1;


}

I've tried using local libraries of glfw and setting the -I and -L locations but nothing has seemed to work. I've made sure the .so and .h files are in their respective locations but I always get this error while running make:

g++ -o output -I/usr/include/GLFW -L/usr/lib/x86_64-linux-gnu -lglfw 

driver.o
driver.o: In function `main':
driver.cpp:(.text+0x5): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'output' failed
make: *** [output] Error 1

I've tried looking at all the other SO posts and they recommend compiling with tons of extra flags, but the only thing I've been able to draw from them is that something is wrong with my library since VScode detects the .h files. How can I compile this without any errors?


Solution

  • Have you tried swapping the linker arguments around? That is, compile with

    g++ -o output driver.o -lglfw
    

    The linker goes through the files from left to right, and it has to know which symbols from libraries you need, before the libraries are processed.