How to link GLFW?

I’m trying to compile a sample OpenGL program – the triangles.cpp in the 9ed of the red book – that uses GLFW on linux. I have got a libglfw3.a by compiling the source with cmake and I’ve specified the linking option -L/the/path/to/the/library, but I still got a lot of error like “undefined reference to `gl3wGenVertexArrays”. Did I miss anything?

PS: I have read the documents on building applications (http://www.glfw.org/docs/latest/build_guide.html), but ‘pkg-config --cflags glfw3’ and ‘pkg-config --static --libs glfw3’ can do nothing but reporting errors.
PS2: I tried to fix pkg-config error by adding directory in PKG_CONFIG_PATH variable or running at the same fold as glfw3.pc, but still the same error.

Thank you for your help.

You mention you’ve specified the link option -L/the/path/to/the/library, but haven’t mentioned if you’ve specified to link to any libraries. You need at minimum to link to -lglfw3 but there is a fairly long list of dependencies and so using cmake or pkg-config should help here.

This is difficult to answer in more detail without more information. Could you provide the command you use to compile & link (either with pkg-config or without), your system OS, and at least the compile/link errors from the output?

Thank you for the reply. I am using NetBeans v8.1 on ubuntu 14.04, 64bit. I also have difficulty in compiling GLFW application on linux command line and in Windows, but let’s focuse on NetBeans first. The linker options are mainly set in the “Linker” tab of the Project Properties dialog (please see the screeenshot below). -L/the/path/to/the/library is set implicitly in the “Additional Library Directories” field and what I input in “Additional Options” is -lglfw3 -pthread -ldl -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11. Then, when I click the “Clean and Build Project” button in the IDE, I got tons of error: undefined reference to ‘gl3wGenVertexArrays’, undefined reference to ‘gl3wBindVertexArray’, undefined reference to ‘gl3wCreateBuffers’, … etc. That is, all GLFW functions are unable to be linked into my application. The libglfw3.a I am using is 245,526 bytes built with typical way – ccmake . , make and make install. Let me know if you need more information. Thank you.

PS: how to use pkg-config? Maybe it can show me correct linker options but it seems that it can do nothing but print lines of errors.
PS2: If you happen to have a libglfw3.a for ubuntu 14.04 64bit x86_64 (I believe it is very commonly used), can you share that with me, in case I had made some mistakes when compiling GLFW? Thanks.

I’ve not used NetBeans to compile C or C++ applications, but looking at the options screen you’ve presented I would have expected the libraries to be added on the line Libraries, without the -l option. Please try this.

For pkg-config, take a look at the documentation and if you get errors using this approach please post the actual command you’ve used along with the actual error output. Although this can be large you can use three back-ticks as per this cheatsheet to enter the error output and it will then be in a scrollable well if lengthy.

Adding libglfw3.a in Libraries field has exactly the same effect of adding -lglfw3 flag in “Additional Options” and produces the same error. NetBeans IDE is simply a GUI wrapper of command lines. The actual command lines NetBeans executes along with error messages are as follows:

g++    -c -g -I/home/me/ComputerGraphics/include -I/home/me/glfw-3.2.1/include -MMD -MP -MF "build/Debug/GNU-Linux/01-triangles.o.d" -o build/Debug/GNU-Linux/01-triangles.o 01-triangles.cpp
mkdir -p dist/Debug/GNU-Linux
g++     -o dist/Debug/GNU-Linux/opengl1 build/Debug/GNU-Linux/01-triangles.o -L/home/me/glfw-3.2.1/bin/lib -Wl,-rpath,/home/me/glfw-3.2.1/bin/lib -lglfw3 -pthread -ldl -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11
build/Debug/GNU-Linux/01-triangles.o: In function `init()':
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:27: undefined reference to `gl3wGenVertexArrays'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:28: undefined reference to `gl3wBindVertexArray'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:35: undefined reference to `gl3wCreateBuffers'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:36: undefined reference to `gl3wBindBuffer'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:37: undefined reference to `gl3wBufferStorage'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:46: undefined reference to `LoadShaders'
......

The pkg-config command line I executed are just those in the GLFW documents on building applications (that is, the documentation you linked: http://www.glfw.org/docs/latest/build_guide.html):

pkg-config --cflags glfw3
pkg-config --static --libs glfw3

Both of them spit out the same error:

Package glfw3 was not found in the pkg-config search path.
Perhaps you should add the directory containing `glfw3.pc'
to the PKG_CONFIG_PATH environment variable
No package 'glfw3' found

I have add the directory containing libglfw3.a to PKG_CONFIG_PATH and I am running right in that folder as I said in the original post. No use. I have to think the source code of pkg-config is nothing but a line of printf of errors.

Any ideas?

LoadShaders and gl3w* functions are not part of GLFW. You need to figure out where in some other place they are defined and link that other C/C++ source file or library.

I thought you’d made a typo in your first post when you mentioned
gl3wGenVertexArrays etc. There is no such function, it’s just
glGenVertexArrays etc.

I don’t think it is a typo. Most likely it is part of some GL loader that comes with book source code.

2 Likes

Yeah, the code is from the 9th edition of “OpenGL Programming Guide” and downloadable from here. So, has anyone of you ever tried compiling samples therein?

OK, I finally solved all problems and successfully built the code … Great thanks to everyone who helped me.

Those interested are referred to my answer here: http://stackoverflow.com/questions/41776746/how-to-set-up-in-netbeans-8-1-to-successfully-compile-an-opengl-code-which-uses/41785882#41785882

I’m glad you’ve solved the problem :slight_smile:

The extra information regarding the source of your examples could have helped us solve this very quickly. I realise you were likely trying to save adding extraneous info, but it’s always a good idea to add as much as you can to any queries.

Yep, it appears gl3w loads the functions as gl3w* then #defines them as gl*.

Note: I didn’t think there was a typo in the code, I thought there was a typo in the post text so sadly didn’t look up gl3w*. Once I saw the actual error output I realised my mistake - which confirms both that I’m can be a bit of an idiot and that it’s useful to quote the actual error output!

Thanks @mmozeiko