jsj
jsj

Reputation: 9421

How do I link GLFW

I am trying to link GLFW to my C program.

The docs seem to suggest #include<GLFW/glfw3.h> however I have installed 2.7.2 (from my distro's repository) and don't have that header file:

find / -name *glfw* 2> /dev/null
/usr/lib/libglfw.so.2.6
/usr/lib/libglfw.a
/usr/lib/libglfw.so
/usr/lib/pkgconfig/libglfw.pc
/usr/lib/libglfw.so.2
/usr/include/GL/glfw.h
/usr/share/doc/libglfw-dev
/usr/share/doc/libglfw2
/var/cache/apt/archives/libglfw2_2.7.2-1_i386.deb
/var/cache/apt/archives/libglfw-dev_2.7.2-1_i386.deb
/var/lib/dpkg/info/libglfw2.list
/var/lib/dpkg/info/libglfw2.postinst
/var/lib/dpkg/info/libglfw-dev.md5sums
/var/lib/dpkg/info/libglfw2.postrm
/var/lib/dpkg/info/libglfw2.md5sums
/var/lib/dpkg/info/libglfw2.shlibs
/var/lib/dpkg/info/libglfw-dev.list

I tried #include<GL/glfw.h> but I still get undefined reference to 'glfwLoadTexture2D'

How do I link to GLFW and use glfwLoadTexture2D()?

Upvotes: 1

Views: 15639

Answers (3)

catloverxx
catloverxx

Reputation: 47

When I am on Linux, I compile opengl/glfw projects like this:

gcc main.c -lGL -lglfw

When I am on windows, I compile them by writing:

gcc main.c libglfw3.a -lopengl32 -lgdi32

and I put libglfw3.a file in the same directory where main.c is. I have read people say that they couldn't link properly before writing -lopengl32 -lgdi32 -luser32 -lkernel32 -lws2_32. Another thing which may be worth mentioning is that I couldn't link glfw libraries when I downloaded 32bit glfw binaries. When I downloaded 64bit glfw binaries everything worked fine. I have a 64 bit machine and a x86_64-w64-mingw32. I have read comments from people with the opposite experience, where they weren't able to link glfw libraries when they downloaded 64bit binaries, but they were able to link them after downloading 32bit binaries. My advice would be to try both.

Upvotes: 1

unwind
unwind

Reputation: 400039

An #include does nothing for the linker; it just brings in declarations, not the actual functions.

The documentation indicates that GLFW uses pkg-config (not surprising; @elmindreda knows her stuff), so your compilation line should be something like:

$ cc `pkg-config --cflags glfw3` -o foo foo.c `pkg-config --static --libs glfw3`

Also note that since the library uses pkg-config, you're not supposed to "care" about details such as where the header and library files are located on your particular installation. Just ask using the --cflags and --libs modes, and you will get the proper locations returned, as the example above indicates.

Upvotes: 3

us2012
us2012

Reputation: 16263

You are mixing up compilation and linking. If you were missing headers, you would probably have errors a lot sooner than the linking stage.

"Undefined reference" results from symbols not being found by the linker. The most likely cause is you not telling gcc that it should link to the GLFW libraries:

 gcc myfile.c -lglfw

Upvotes: 1

Related Questions