Search code examples
linuxbuildinstallationcmakeglfw

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


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.


Solution

  • 2020 Updated Answer

    It is 2020 (7 years later) and I have learned more about Linux during this time. Specifically that it might not be a good idea to run sudo make install when installing libraries, as these may interfere with the package management system. (In this case apt as I am using Debian 10.)

    If this is not correct, please correct me in the comments.

    Alternative proposed solution

    This information is taken from the GLFW docs, however I have expanded/streamlined the information which is relevant to Linux users.

    • Go to home directory and clone glfw repository from github
    cd ~
    git clone https://github.com/glfw/glfw.git
    cd glfw
    
    • You can at this point create a build directory and follow the instructions here (glfw build instructions), however I chose not to do that. The following command still seems to work in 2020 however it is not explicitly stated by the glfw online instructions.
    cmake -G "Unix Makefiles"
    
    • You may need to run sudo apt-get build-dep glfw3 before (?). I ran both this command and sudo apt install xorg-dev as per the instructions.

    • Finally run make

    • Now in your project directory, do the following. (Go to your project which uses the glfw libs)

    • Create a CMakeLists.txt, mine looks like this

    CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
    PROJECT(project)
    
    SET(CMAKE_CXX_STANDARD 14)
    SET(CMAKE_BUILD_TYPE DEBUG)
    
    set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
    set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
    set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
    
    add_subdirectory(/home/<user>/glfw /home/<user>/glfw/src)
    
    
    FIND_PACKAGE(OpenGL REQUIRED)
    
    SET(SOURCE_FILES main.cpp)
    
    ADD_EXECUTABLE(project ${SOURCE_FILES})
    TARGET_LINK_LIBRARIES(project glfw)
    TARGET_LINK_LIBRARIES(project OpenGL::GL)
    
    • If you don't like CMake then I appologize but in my opinion it is the easiest way to get your project working quickly. I would recommend learning to use it, at least to a basic level. Regretably I do not know of any good CMake tutorial

    • Then do cmake . and make, your project should be built and linked against glfw3 shared lib

    • There is some way of creating a dynamic linked lib. I believe I have used the static method here. Please comment / add a section in this answer below if you know more than I do

    • This should work on other systems, if not let me know and I will help if I am able to