简体   繁体   中英

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?

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.

All is perfectly explained in the manual https://www.glfw.org/docs/latest/build_guide.html#build_link_pkgconfig

The key problem is in your -I/usr/include/GLFW and #include <GLFW/glfw3.h> that gives in sum the path /usr/include/GLFW/GLFW/glfw3.h . I suppose this is a wrong path to glfw3.h . compilation was successful because of the system default include path -I/usr/include .

Do not tune compiler flags manually, let pkg-config do it for you.

A typical compile and link command-line when using the static version of the GLFW library may look like this:

 g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --static --libs glfw3`

If you are using the shared version of the GLFW library, simply omit the --static flag.

 g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --libs glfw3`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM