Building and compiling GLFW on OSX with CMake?

I would like to build GLFW with CMake on OSX, no XCode, no Visual Studio etc. The purpose is to have GLFW in a custom location where I can link it against any of my projects.

So I downloaded the *.zip file and extracted it inside:
~/.tmp/glfw-3.3.6

Then I created a build folder inside the folder listed above, and within that folder I have executed the following command:
cmake -S /Users/me/.tmp/glfw-3.3.6 -B /Users/me/.tmp/glfw-3.3.6/build

Which seems to have worked, as I did not get any errors. Then I have executed:
make

And that also seems to have worked, no errors. But that is where the instructions in the documentation section of GLFW end.

Where are the header files and the library files to link against?

With a last bit of desperation, I typed make install and the process chucked bunch of files into /usr/local which was definitely the one thing I didn’t want to happen!

-- Installing: /usr/local/lib/libglfw3.a
-- Installing: /usr/local/include/GLFW
-- Installing: /usr/local/include/GLFW/glfw3.h
-- Installing: /usr/local/include/GLFW/glfw3native.h
-- Installing: /usr/local/lib/cmake/glfw3/glfw3Config.cmake
-- Installing: /usr/local/lib/cmake/glfw3/glfw3ConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets.cmake
-- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets-noconfig.cmake
-- Installing: /usr/local/lib/pkgconfig/glfw3.pc

Is that it? I wanted to have GLFW in my custom location but there is nothing about this in the documentation?

If you want to install GLFW using CMake into a custom directory check out the CMAKE_INSTALL_PREFIX documentation:
https://cmake.org/cmake/help/v3.0/variable/CMAKE_INSTALL_PREFIX.html

Alternatively you could use GLFW as a subrepository of your projects, which is what I would normally recommend as this makes setup across multiple machines much easier. For an example of this checkout this GLFW CMake Stater:

This example has a single project linking to GLFW, but you can have multiple projects under individual directories with a cmake script referencing these, or describe the multiple targets in one cmake file.

1 Like