Hello again blog… we really have to stop doing this as I should be updating you regularly. I mean, what’s the point of paying for a fancy domain name if we’re not going to do anything with you?
So I’m back after a long hiatus, the last post before which I wrote a lot about Margaret Thatcher. What’s happened since then? Not much really… got on with work, got on with life – the usual. That said, I’m beginning to think about how I can renovate this place to become my online home, which I intended to in the first place. Stick a few links to my github up, to Twitter and all that… and we’re laughing. But having just renewed my domain with WordPress I’m not entirely sure if I can point to something else or hack my WordPress layout to look a bit more like what I’m imagining right now.
One thing I have been doing during the hiatus is a lot of coding. Some projects haven’t seen the light of day, others such as my attempts at making a 3D engine have been posted up, along with an old pong game I made ages ago and recently rescued from oblivion. Might as well have it up on github, as a) I’ve lost one or two Linux installations in my time (don’t set Clang as your default compiler on Linux… nVidia drivers hate it!) and b) well, eventually I hope to have built up a decent portfolio of stuff. I’ve also learned a lot about the different frameworks that you can use on Linux, and I’ve found that SDL 2 is all-encompassing, but complicated and not necessarily what you want just for a few demos, GLUT is getting on a bit, and GLFW… GLFW seems to tick all the boxes.
However, even using GLFW has its problems on Ubuntu – namely that the packaging of many libraries is not that quick, and in particular GLFW is stuck back in version 2.x when the rest of the GLFW community is on GLFW 3. This could be a problem, although thanks to the magic of Linux you can get it working and keep up to date… well, in theory. Although it’s gotten a lot easier you still can be in for a world of pain if the dependencies aren’t there and the compiler shouts at you. I got shouted at to start with, but then I learned to stop worrying and love the CMake file and -hey presto- it worked! However, even compiling the test after the damn thing is installed is somewhat complicated, and so I’m writing a blog post so I don’t forget if I ever need to do it again, and that others don’t have to put up with the Googling needed once they find this (hopefully).
Also I’m thinking that probably this will work for most Linuxes with minor modification.
So first, setting up your system: you’ll need CMake and git to start with. Once you’ve installed those from the repositories, go to the GLFW github site, or just clone it by cd’ing to whatever directory you want to keep it at, and then type:
git clone https://github.com/glfw/glfw.git
After git has cloned it, cd into the glfw directory. Now here’s where a bit of personal preference comes in… I prefer doing out-of-source builds with CMake as I’ve found when I’ve just typed “cmake” in a directory things get cluttered. So do whatever works for you here, but I usually create the build directory as it’s easy to burn if the whole thing messes up.
One problem that stumped me initially was that doxygen seemed to throw a strop every time I tried to build it and hold up the whole make process, so this can be cured by opening the CMakeLists.txt file in your favourite text editor, and setting:
option(GLFW_BUILD_DOCS "Build the GLFW documentation" OFF)
This means CMake won’t include doxygen in there, and for now makes your life easier. I might go back and look at it if I have the time, although documentation of GLFW is plentiful online so in my world this just gets rid of a big problem. So, anyway… then go to your build directory (or whereever you’re doing it) and type:
You’ll see a lot of compiler feedback, and hopefully it will just be telling you how everything has been built. Aside from doxygen, most of the dependencies such as GLX and pthread should be included by default on your system. Once that’s done (and how quickly it builds, at least for me!) type:
sudo make install
From experience, I guess Fedora-based folk should type
make install, but same thing – it should install all of the libraries to the right place. This, however, is the easy part. Now if you want to test it, you have to compile and link one of the examples. I chose
simple.c, but you can’t just type:
gcc -o simple simple.c -lGL -lGLU -lglfw3
like you might think, oh no. You’ll get a lot of error messages about things called XClipboard and pow and all manner of weird crap. I guess Ubuntu libraries are packaged to avoid this but if you’ve compiled from source to get your libraries, you have to type:
gcc simple.c -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lm
which links against the GL libraries, as well as a load of X.org stuff, the Unix threading library and the math library. If all is well with these, you should then see the glory of the test program…
EDIT: Yeah, that way actually kinda sucks. If your system has pkg-config on it, then you don’t have to go to so much trouble remembering to link to lX11, etc. Instead, just do this:
gcc `pkg-config --cflags glfw3` -o simple simple.c `pkg-config --static --libs glfw3`
and if you built shared (which I found caused me no end of annoying problems… might want to fix that and put it back) just omit the
pkg-config pretty much takes all the pain out. CMake also has a pkg-config module as well… though I’m going to cover that some other point. Been wanting to fix this for a while…
And if you’re seeing this: congratualtions! You’ve successfully compiled and linked GLFW 3! Although you might want to use CMake or at the very least a makefile in future to keep track of all those libraries!
One thought on “building glfw 3 on ubuntu 13.10 (or how i learned to stop worrying and love the cmake file)”
Man, you saved my time.Thank you very much and kudos from Russia!