How to build & install GLFW 3 and use it in a Linux project


Answers

Note that you do not need that many -ls if you install glfw with the BUILD_SHARED_LIBS option. (You can enable this option by running ccmake first).
This way sudo make install will install the shared library in /usr/local/lib/ligglfw.so. You can then compile the example file with a simple:

g++ main.cpp -L /usr/local/lib/ -lglfw

Then do not forget to add /usr/local/lib/ to the search path for shared libraries before running your program. This can be done using:

export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}

And you can put that in your ~/.bashrc so you don't have to type it all the time.

Question

GLFW3

Last night I was working late trying to build the GLFW 3 packages for Linux from source. This process took me a very long time, about 3 hours in total, partly because I am unfamiliar with CMake, and partly because I am was unfamiliar with GLFW.

I hope that this post will save you from the difficulty I had yesterday! I thought I should make a short write-up, and hopefully save you several hours of your life...

Thanks to "urraka", "b6" and "niklas" on the #glfw IRC channel, I have been able to get glfw version 3.0.1 working.

It turns out this is not a trivial process (certainly not for me, I am no expert) as there is not much documentation on the web about glfw3, particularly about setting it up with CMake.

I was asked to split this into a question and answer section, and so I have done that, and the answer parts are now below.

Are you a maintainer of GLFW, or a member of the GLFW team?

If any of the maintainers of GLFW3 see this, then my message to them is please add a "setting up GLFW3 on Windows, Mac OS X and Linux" section to your website! It is quite easy to write programs with GLFW, since the online documentation is quite good, a quick scan of all the available classes and modules and you'll be ready to go. The example of a test project featured here is also very good. The two main problems I found were, firstly how do I set up GLFW3 on my system, and secondly how to I build a GLFW3 project? These two things perhaps aren't quite clear enough for a non-expert.

Edit

Had a brief look today (Date: 2014-01-14) it looks as if the GLFW website has undergone heavy changes since I last looked and there is now a section on compiling GLFW and buliding programs with GLFW, which I think are new.




GLFW3 - Undefined reference to XRR

I figured out the answer myself while writing the question.

I was misled into believing that I was linking everything necessary because of the output of

pkg-config --libs --cflags --print-requires glfw3 

which was

-I/usr/local/include  -L/usr/local/lib -lglfw3  

The --print-requires flag was having no impact at all on the output, which seemed odd. I searched and printed the corresponding .pc file.

sudo find / | grep "glfw3\.pc"
cat /usr/local/lib/pkgconfig/glfw3.pc 

There I found this.

Requires.private:  x11 xrandr xi xxf86vm gl

Which indicates which libraries are required for static linking. I added their correponding flags to CMake and it worked. My mistake was that I missed the --print-requires-private flag when executing pkg-config.

I hope this helps someone save some time.




What is the difference between LD_LIBRARY_PATH and -L at link time?

The settings of LD_LIBRARY_PATH has the highest precedence, so when it is set, the set of directories mentioned by LD_LIBRARY_PATH are searched first even before the standard set
of directories. So in your case setting of LD_LIBRARY_PATH is influencing the lookup of
the libraries mentioned with -l option. Without LD_LIBRARY_PATH some of the dependencies
might have been resolved from the standard set of directories.

Though setting of LD_LIBRARY_PATH help with debugging and to try out a newer version of
a library its usage in the general development environment setup and deployment is considered bad.

Also refer this HOWTO from Linux Documentation for more details on Shared Libraries




LD_LIBRARY_PATH is intended for finding shared libraries when running an application. It is a side effect that it's impacting your link, and you should not rely on that.

As an often unwanted side effect, LD_LIBRARY_PATH will also be searched at link (ld) stage after directories specified with -L (also if no -L flag is given).

Why LD_LIBRARY_PATH is bad




Great guide, thank you. Given most instructions here, it almost built for me but I did have one remaining error.

/usr/bin/ld: //usr/local/lib/libglfw3.a(glx_context.c.o): undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

After searching for this error, I had to add -ldl to the command line.

g++ main.cpp -lglfw3 -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -lpthread -ldl

Then the "hello GLFW" sample app compiled and linked.

I am pretty new to linux so I am not completely certain what exactly this extra library does... other than fix my linking error. I do see that cmd line switch in the post above, however.