GLFW – Compiling Source and Creating an Xcode Project

GLFW is a great library – easy to use, great documentation covering it’s API design. But if you’re not on Windows where they provide pre-built libraries, going from a source download to a compiling Xcode project is _not_easy.

Since brew (as of writing) only provides GLFW 2, you need to build GLFW 3 yourself.

There is barely any information on it at all, it assumes you know exactly what you’ve downloaded (the source and some CMake files btw) and that you’ll be able to figure it all out (there is some additional information if you root around the GitHub forums but it’s not exactly easy to find).

So I’m posting up how I got GLFW to compile and running in Xcode here in-case anyone else starts banging their head against a brick wall.

First off, download the source followed by Cmake (I downloaded the 64/32-bit .dmg for 2.8.11.2).

Now to get it to compile you need to set up a few options. You can do this in the Cmake GUI, but I found it much easier to do it manually. Browse to GLFW/Src and open config.h.in.

This is a settings file for the compile you’re about to perform. There are a lot of settings in here you can play around with (they are explained in the README.md file in the root GLFW folder), but I enabled the following

  • _GLFW_COCOA
  • _GLFW_NSGL
  • _GLFW_NO_DLOAD_WINMM (this is a Windows only define, but I enabled it so adding it here anyway)
  • _GLFW_USE_OPENGL

 

Save the file and then you’re ready to compile.

Open the Terminal and browse to the root of the GLFW folder and enter the following commands

cmake .
make install

The Cmake command will set up the project ready to be built. It’ll take the config file we modified earlier and create a valid config.h file and it’ll carry out any other set up needed as well.  Calling make builds the project, the install command installs the library in it’s default location.

Now you’re ready to add GLFW to your Xcode project. To keep it simple I’ll step through adding it to a new project, once you can do that adding it to an existing one is pretty easy.

  • Open Xcode and create a new OSX Command Line Tool project
  • In the project settings add the path to the GLFW include file in the ‘Header Search Paths’ and the path to the libglfw3.a lib to the ‘Library Search Paths’ option (both of these paths are shown in the install make output)
  • Add libglfw3.a to the Link build phase
  • You also need to add the following Frameworks to the build phase to allow the project to compile
    • IOKit
    • Cocoa
    • OpenGL

 

You can now copy/paste the sample code provided on the GLFW website into the existing main.c file, hit compile and you have a running GLFW project.

Note that the sample code doesn’t call glclear before glfwSwapBuffers so what you get in the open window is undefined.

23 comments

  1. Thanks so much mate, really useful guide to getting this set up. Although I didn’t go for the command line Cmake GUI and built everything including all the extra cruft.
    I needed the assistance with getting this set up in a new xCode project as I’m usually using xCode to create iOS projects.
    Really nice guide so kudos.

  2. I wanted to ask what OS you’re on?
    I’m having a bit of trouble, I’ve cmake’ed glfw, but when I call “make” it give out this error – “glfw3.h:183:27: error: OpenGL/gl.h: No such file or directory”. I tried searching around for where OpenGL headers are stored and it pointed me to – “System/Library/Frameworks/OpenGL.Framework”, well, like the error suggests I find the headers. I’m wondering if OpenGL is somewhat messed up on my machine?

  3. Hi Matt, I’ve been using XCode 5 for a while now and I’ve not had a problem with building any of the libraries. I assume the problem you’re having is finding GLFW/glfw3.h after you’ve called ‘make install’?

    If that’s the case, I assume you’ve not pointed XCode to the right header search path? Check what the ‘make install’ command outputs and check the path has been added correctly (you want to add the root path of the include path).

  4. Hi Lee Winder,
    i want using glfw3,
    compile with cmake-2.8.12-Darwin64-universal,
    on Mac OS 10.6 with XCode 3.

    I was follow your guide, but got error when doing make install.
    I’m not sure if i’m doing correctly to enabled _GLFW_COCOA, _GLFW_NSGL, _GLFW_USE_OPENGL inside GLFW/Src/config.h.in file.

    To enabled it, i’ve just delete the # symbol, isn’t it?
    But regarding to following error message, it looks I’ve forgot to add some semicolon or something.

    At the end my post, i put my config.h.in .
    Where is my mistakes? How to correct it?

    Regards,
    Geo

    * Error Message *
    Scanning dependencies of target glfw
    [ 2%] Building C object src/CMakeFiles/glfw.dir/clipboard.c.o
    In file included from /Volumes/Development/Softwares/glfw-3.0.3/src/internal.h:32,
    from /Volumes/Development/Softwares/glfw-3.0.3/src/clipboard.c:27:
    /Volumes/Development/Softwares/glfw-3.0.3/myBuild/src/config.h:40: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_GLFW_COCOA’
    In file included from /Volumes/Development/Softwares/glfw-3.0.3/src/clipboard.c:27:
    /Volumes/Development/Softwares/glfw-3.0.3/src/internal.h:41:3: error: #error “No supported client library selected”
    /Volumes/Development/Softwares/glfw-3.0.3/src/internal.h:71:3: error: #error “No supported window creation API selected”
    In file included from /Volumes/Development/Softwares/glfw-3.0.3/src/clipboard.c:27:
    /Volumes/Development/Softwares/glfw-3.0.3/src/internal.h:186: error: expected specifier-qualifier-list before ‘_GLFW_PLATFORM_FBCONFIG’
    /Volumes/Development/Softwares/glfw-3.0.3/src/internal.h:241: error: expected specifier-qualifier-list before ‘_GLFW_PLATFORM_WINDOW_STATE’
    /Volumes/Development/Softwares/glfw-3.0.3/src/internal.h:264: error: expected specifier-qualifier-list before ‘_GLFW_PLATFORM_MONITOR_STATE’
    /Volumes/Development/Softwares/glfw-3.0.3/src/internal.h:313: error: expected specifier-qualifier-list before ‘_GLFW_PLATFORM_LIBRARY_WINDOW_STATE’
    make[2]: *** [src/CMakeFiles/glfw.dir/clipboard.c.o] Error 1
    make[1]: *** [src/CMakeFiles/glfw.dir/all] Error 2
    make: *** [all] Error 2

    * config.h.in *
    // Define this to 1 if building GLFW for Cocoa
    cmakedefine _GLFW_COCOA
    // Define this to 1 if building GLFW for NSGL
    cmakedefine _GLFW_NSGL
    // Define this to 1 if using OpenGL as the client library
    cmakedefine _GLFW_USE_OPENGL

  5. Hi Geo, to enable the setting in config.h.in you need to define the value as 1, rather than remove the #.

    So the line
    #cmakedefine _GLFW_COCOA

    becomes
    #cmakedefine _GLFW_COCOA 1

  6. Hi Lee,

    Thank you for the guide. They’re very helpful.

    I followed your instruction until this step

    >> Add libglfw3.a to the Link build phase

    This was when I got into the problem. I couldn’t find “libglfw3.a” in the list of frameworks/libraries. Even when I chose “Add Other…”, I still couldn’t target the /usr/local/lib/libglfw3.a. (From the terminal, I saw that “libglfw3.a” was put under “/usr/local/lib/” directory)

    I’m using Xcode 5. So, how do I add “libglfw3.a” into the Link Binary list?

    Thank you for your time.

  7. I got it. I just simply copied the libglfw3.a to another folder on my machine, then link to it, and every thing worked out great. Silly me.

    Thank you.

    1. You can manually add the file

      Heres the trick
      1) Show hidden folders. Paste this into terminal: defaults write com.apple.finder AppleShowAllFiles YES
      2) Relaunch a finder window and navigate to Macintosh HD\usr
      3) Drag the usr folder to the “Favorites” side in the terminal
      4) In xcode, click the add “+” to link binaries, then click “Add Other”
      5) navigate to the libglfw3.a and add it!

  8. Hi,

    It’s me again. This time, I encountered another problem with glfw3.

    Basically, this is the problem:

    —————————–
    glGenVertexArrays(1, &VAO_obj); // Error : Use of undeclared identifier ‘glGenVertexArrays’
    glBindVertexArray(VAO_obj); // Error : Use of undeclared identifier ‘glBindVertexArray’

    glGenBuffers(1, &VAO_obj); // Worked just fine!
    glBindBuffer(GL_ARRAY_BUFFER, VAO_obj); // Worked just fine!

    ————————

    So, it seems that the glfw3 library is missing a couple of functions. I’m just wondering if any one has encountered this issue?

    1. For anyone else coming here years later via a google search, if you
      #define GLFW_INCLUDE_GLCOREARB 1
      before you
      #include

      Then it works, without gl.h/gl3.h confusion.

  9. it seemed that at this time, glfw3 didn’t include . After I include gl3.h, it worked fine. You’ll get a warning that both “gl.h” and “gl3.h” include though, but since glfw3 automatically include “gl.h”, there’s no other way to get around it if you decide to go with glfw3.

  10. Hi I followed this through all the way to the creating xcode project where I received some errors.
    – I added the following path to Header search paths: /usr/local/include/GLFW/glfw3.h (as specified by the make install.
    – I added the following path to Library Search paths: /usr/local/lib/libglfw3.a (as specified by the make install)
    – The following frameworks are included in my Link Binary with Libraries in ‘Build Phases’:
    OpenGl.framework
    IOKit.framework
    Cocoa.framework
    libglfw3.a

    yet when I copy/paste the sample code I get a build failure and the following error – GLFW/glfw3.h file not found.

    **I double checked and glfw3.h is indeed at location /usr/local/include/GLFW/glfw3.h

    Any idea how to fix this issue?

    Thanks!

  11. I fixed this – I am relatively new to this and I didn’t realize my Search paths should be to directories and not files.

  12. I got some undeclared identifier errors for CVDisplayLinkCreateWithCGDisplay and related symbols. After adding QuartzCore Framework in build phase it worked fine. Thanks for the doc !!

  13. Hi, when I make install, i get this:

    make[2]: *** [src/CMakeFiles/glfw.dir/clipboard.c.o] Error 1
    make[1]: *** [src/CMakeFiles/glfw.dir/all] Error 2
    make: *** [all] Error 2

    Any idea why this is?

  14. Thanks a lot Lee! I spent hours trying to get a decent library to work until I found your blog. Here are some comments others may find useful:

    1) the root of the GLFW folder was not totally clear to me and Cmake was not finding the right files ( I downloaded version 3.04) . In my case the right path to run $ Cmake .. was
    /Users/myname/Library/glfw-3.0.4/build
    2) after Cmake completed I ran make install and had a permission error. I ran it successfuly a second time using the terminal and
    $ sudo make install
    3) In Xcode 5.0 the ‘Header Search Paths’ and ‘Library Search Paths’ are under the ‘Build Settings’ group and the Link Binary under ‘Build Phases’ (this may be obvious to many but it was not no me). The paths that worked for me were:
    /Users/myname/Library/glfw-3.0.4/include for Header
    /Users/myname/Library/glfw-3.0.4/build/src for Library
    /Users/myname/Library/glfw-3.0.4/build/src for the Binary Link to libglfw3.a

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s