How to build and use GLFW

GLFW is a library for OpenGL created by Camilla Berglund. It is a modern replacement to GLUT, to draw windows and handle input.

GLFW 2.x

Ubuntu ships with an ancient version of GLFW that can be installed easily:

$ sudo apt install libglfw-dev

However, it is highly recommended to use GLFW 3.x. It has a different API and your source code and build options will need to be changed to use it.

To remove this ancient GLFW:

$ sudo apt remove libglfw-dev glfw2

GLFW 3.x

I like to install GLFW from Github. You could also get one of its stable releases, the installation steps would be the same for either.

  • Get the source:
$ git clone https://github.com/glfw/glfw.git
  • Build the library. Note that we request CMake to create shared library files. Why this is not the default on Linux puzzles me!
$ cd glfw
$ mkdir build
$ cd build
$ cmake -D BUILD_SHARED_LIBS=ON ..
  • Install the library:
$ sudo make install

Note that I highly recommend using checkinstall as described here instead of doing this.

  • Update the dynamic linker cache, so that the libglfw.so shared library file can be loaded at runtime:
$ sudo ldconfig

If you do not do this, you get an error similar to that described here.

  • To use GLFW 3.x calls in your code, include the header file:
#include <GLFW/glfw3.h>
  • To link and build your code use the library directive: -lglfw to your build options or CMake.

Tried with: GLFW 20150825 and Ubuntu 14.04

Advertisements

How to add library directory to ldconfig cache

Problem

ldconfig is a program that is used to maintain the shared library cache. This cache is typically stored in the file /etc/ld.so.cache and is used by the system to map a shared library name to the location of the corresponding shared library file. This is used when a program is executed to find locations of shared libraries that need to be dynamically loaded and linked to the program. By default, the shared library files in /lib, /usr/lib and a few other standard directories are maintained in the cache by ldconfig.

A new program or library might be installed in a non-standard directory, for example in /opt/foobar/lib. Programs that need the shared libraries from this library might fail with this error when executed:

hello-world-program: error while loading shared libraries: libFoobar.so.1: cannot open shared object file: No such file or directory

The libFoobar.so.1 might be located in /opt/foobar/lib, but the system does not know this, so it cannot successfully load and execute the program.

Solution

To fix this problem, we need to add the library directory to the list used by ldconfig. There are two ways to do this: just add to LD_LIBRARY_PATH or rebuild cache.

Add to LD_LIBRARY_PATH

Set this at shell for temporary use or add to the shell initialization file for permanent effect:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/foobar/lib

Rebuild cache

  • Open the /etc/ld.so.conf as sudo and add a new line with the library directory. In this case, we add /opt/foobar/lib.

  • Rerun ldconfig to rebuild the cache:

$ sudo ldconfig
  • Check if the shared library cache now includes the shared libraries from the new directory:
$ ldconfig -p

Your program should now execute without any errors 🙂

Tried with: Ubuntu 14.04