1# Compiling GLFW {#compile_guide} 2 3[TOC] 4 5This is about compiling the GLFW library itself. For information on how to 6build applications that use GLFW, see @ref build_guide. 7 8GLFW uses some C99 features and does not support Visual Studio 2012 and earlier. 9 10 11## Using CMake {#compile_cmake} 12 13GLFW behaves like most other libraries that use CMake so this guide mostly 14describes the standard configure, generate and compile sequence. If you are already 15familiar with this from other projects, you may want to focus on the @ref 16compile_deps and @ref compile_options sections for GLFW-specific information. 17 18GLFW uses [CMake](https://cmake.org/) to generate project files or makefiles 19for your chosen development environment. To compile GLFW, first generate these 20files with CMake and then use them to compile the GLFW library. 21 22If you are on Windows and macOS you can [download 23CMake](https://cmake.org/download/) from their site. 24 25If you are on a Unix-like system such as Linux, FreeBSD or Cygwin or have 26a package system like Fink, MacPorts or Homebrew, you can install its CMake 27package. 28 29CMake is a complex tool and this guide will only show a few of the possible ways 30to set up and compile GLFW. The CMake project has their own much more detailed 31[CMake user guide][cmake-guide] that includes everything in this guide not 32specific to GLFW. It may be a useful companion to this one. 33 34[cmake-guide]: https://cmake.org/cmake/help/latest/guide/user-interaction/ 35 36 37### Installing dependencies {#compile_deps} 38 39The C/C++ development environments in Visual Studio, Xcode and MinGW come with 40all necessary dependencies for compiling GLFW, but on Unix-like systems like 41Linux and FreeBSD you will need a few extra packages. 42 43 44#### Dependencies for Wayland and X11 {#compile_deps_wayland} 45 46By default, both the Wayland and X11 backends are enabled on Linux and other Unix-like 47systems (except macOS). To disable one or both of these, set the @ref GLFW_BUILD_WAYLAND 48or @ref GLFW_BUILD_X11 CMake options in the next step when generating build files. 49 50To compile GLFW for both Wayland and X11, you need to have the X11, Wayland and xkbcommon 51development packages installed. On some systems a few other packages are also required. 52None of the development packages above are needed to build or run programs that use an 53already compiled GLFW library. 54 55On Debian and derivatives like Ubuntu and Linux Mint you will need the `libwayland-dev` 56and `libxkbcommon-dev` packages to compile for Wayland and the `xorg-dev` meta-package to 57compile for X11. These will pull in all other dependencies. 58 59```sh 60sudo apt install libwayland-dev libxkbcommon-dev xorg-dev 61``` 62 63On Fedora and derivatives like Red Hat you will need the `wayland-devel` and 64`libxkbcommon-devel` packages to compile for Wayland and the `libXcursor-devel`, 65`libXi-devel`, `libXinerama-devel` and `libXrandr-devel` packages to compile for X11. 66These will pull in all other dependencies. 67 68```sh 69sudo dnf install wayland-devel libxkbcommon-devel libXcursor-devel libXi-devel libXinerama-devel libXrandr-devel 70``` 71 72On FreeBSD you will need the `wayland`, `libxkbcommon` and `evdev-proto` packages to 73compile for Wayland. The X11 headers are installed along the end-user X11 packages, so if 74you have an X server running you should have the headers as well. If not, install the 75`xorgproto` package to compile for X11. 76 77```sh 78pkg install wayland libxkbcommon evdev-proto xorgproto 79``` 80 81On Cygwin Wayland is not supported but you will need the `libXcursor-devel`, 82`libXi-devel`, `libXinerama-devel`, `libXrandr-devel` and `libXrender-devel` packages to 83compile for X11. These can be found in the Libs section of the GUI installer and will 84pull in all other dependencies. 85 86Once you have the required dependencies, move on to @ref compile_generate. 87 88 89### Generating build files with CMake {#compile_generate} 90 91Once you have all necessary dependencies it is time to generate the project 92files or makefiles for your development environment. CMake needs two paths for 93this: 94 95 - the path to the root directory of the GLFW source tree (not its `src` 96 subdirectory) 97 - the path to the directory where the generated build files and compiled 98 binaries will be placed 99 100If these are the same, it is called an in-tree build, otherwise it is called an 101out-of-tree build. 102 103Out-of-tree builds are recommended as they avoid cluttering up the source tree. 104They also allow you to have several build directories for different 105configurations all using the same source tree. 106 107A common pattern when building a single configuration is to have a build 108directory named `build` in the root of the source tree. 109 110 111#### Generating with the CMake GUI {#compile_generate_gui} 112 113Start the CMake GUI and set the paths to the source and build directories 114described above. Then press _Configure_ and _Generate_. 115 116If you wish change any CMake variables in the list, press _Configure_ and then 117_Generate_ to have the new values take effect. The variable list will be 118populated after the first configure step. 119 120By default, GLFW will use Wayland and X11 on Linux and other Unix-like systems other than 121macOS. To disable support for one or both of these, set the @ref GLFW_BUILD_WAYLAND 122and/or @ref GLFW_BUILD_X11 option in the GLFW section of the variable list, then apply the 123new value as described above. 124 125Once you have generated the project files or makefiles for your chosen 126development environment, move on to @ref compile_compile. 127 128 129#### Generating with command-line CMake {#compile_generate_cli} 130 131To make a build directory, pass the source and build directories to the `cmake` 132command. These can be relative or absolute paths. The build directory is 133created if it doesn't already exist. 134 135```sh 136cmake -S path/to/glfw -B path/to/build 137``` 138 139It is common to name the build directory `build` and place it in the root of the 140source tree when only planning to build a single configuration. 141 142```sh 143cd path/to/glfw 144cmake -S . -B build 145``` 146 147Without other flags these will generate Visual Studio project files on Windows 148and makefiles on other platforms. You can choose other targets using the `-G` 149flag. 150 151```sh 152cmake -S path/to/glfw -B path/to/build -G Xcode 153``` 154 155By default, GLFW will use Wayland and X11 on Linux and other Unix-like systems other than 156macOS. To disable support for one or both of these, set the @ref GLFW_BUILD_WAYLAND 157and/or @ref GLFW_BUILD_X11 CMake option. 158 159```sh 160cmake -S path/to/glfw -B path/to/build -D GLFW_BUILD_X11=0 161``` 162 163Once you have generated the project files or makefiles for your chosen 164development environment, move on to @ref compile_compile. 165 166 167### Compiling the library {#compile_compile} 168 169You should now have all required dependencies and the project files or makefiles 170necessary to compile GLFW. Go ahead and compile the actual GLFW library with 171these files as you would with any other project. 172 173With Visual Studio open `GLFW.sln` and use the Build menu. With Xcode open 174`GLFW.xcodeproj` and use the Project menu. 175 176With Linux, macOS and other forms of Unix, run `make`. 177 178```sh 179cd path/to/build 180make 181``` 182 183With MinGW, it is `mingw32-make`. 184 185```sh 186cd path/to/build 187mingw32-make 188``` 189 190Any CMake build directory can also be built with the `cmake` command and the 191`--build` flag. 192 193```sh 194cmake --build path/to/build 195``` 196 197This will run the platform specific build tool the directory was generated for. 198 199Once the GLFW library is compiled you are ready to build your application, 200linking it to the GLFW library. See @ref build_guide for more information. 201 202 203## CMake options {#compile_options} 204 205The CMake files for GLFW provide a number of options, although not all are 206available on all supported platforms. Some of these are de facto standards 207among projects using CMake and so have no `GLFW_` prefix. 208 209If you are using the GUI version of CMake, these are listed and can be changed 210from there. If you are using the command-line version of CMake you can use the 211`ccmake` ncurses GUI to set options. Some package systems like Ubuntu and other 212distributions based on Debian GNU/Linux have this tool in a separate 213`cmake-curses-gui` package. 214 215Finally, if you don't want to use any GUI, you can set options from the `cmake` 216command-line with the `-D` flag. 217 218```sh 219cmake -S path/to/glfw -B path/to/build -D BUILD_SHARED_LIBS=ON 220``` 221 222 223### Shared CMake options {#compile_options_shared} 224 225@anchor BUILD_SHARED_LIBS 226__BUILD_SHARED_LIBS__ determines whether GLFW is built as a static library or as 227a DLL / shared library / dynamic library. This is disabled by default, 228producing a static GLFW library. This variable has no `GLFW_` prefix because it 229is defined by CMake. If you want to change the library only for GLFW when it is 230part of a larger project, see @ref GLFW_LIBRARY_TYPE. 231 232@anchor GLFW_LIBRARY_TYPE 233__GLFW_LIBRARY_TYPE__ allows you to override @ref BUILD_SHARED_LIBS only for 234GLFW, without affecting other libraries in a larger project. When set, the 235value of this option must be a valid CMake library type. Set it to `STATIC` to 236build GLFW as a static library, `SHARED` to build it as a shared library 237/ dynamic library / DLL, or `OBJECT` to make GLFW a CMake object library. 238 239@anchor GLFW_BUILD_EXAMPLES 240__GLFW_BUILD_EXAMPLES__ determines whether the GLFW examples are built 241along with the library. This is enabled by default unless GLFW is being built 242as a subproject of a larger CMake project. 243 244@anchor GLFW_BUILD_TESTS 245__GLFW_BUILD_TESTS__ determines whether the GLFW test programs are 246built along with the library. This is enabled by default unless GLFW is being 247built as a subproject of a larger CMake project. 248 249@anchor GLFW_BUILD_DOCS 250__GLFW_BUILD_DOCS__ determines whether the GLFW documentation is built along 251with the library. This is enabled by default if 252[Doxygen](https://www.doxygen.nl/) is found by CMake during configuration. 253 254 255### Win32 specific CMake options {#compile_options_win32} 256 257@anchor GLFW_BUILD_WIN32 258__GLFW_BUILD_WIN32__ determines whether to include support for Win32 when compiling the 259library. This option is only available when compiling for Windows. This is enabled by 260default. 261 262@anchor USE_MSVC_RUNTIME_LIBRARY_DLL 263__USE_MSVC_RUNTIME_LIBRARY_DLL__ determines whether to use the DLL version or the 264static library version of the Visual C++ runtime library. When enabled, the 265DLL version of the Visual C++ library is used. This is enabled by default. 266 267On CMake 3.15 and later you can set the standard CMake [CMAKE_MSVC_RUNTIME_LIBRARY][] 268variable instead of this GLFW-specific option. 269 270[CMAKE_MSVC_RUNTIME_LIBRARY]: https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html 271 272@anchor GLFW_USE_HYBRID_HPG 273__GLFW_USE_HYBRID_HPG__ determines whether to export the `NvOptimusEnablement` and 274`AmdPowerXpressRequestHighPerformance` symbols, which force the use of the 275high-performance GPU on Nvidia Optimus and AMD PowerXpress systems. These symbols 276need to be exported by the EXE to be detected by the driver, so the override 277will not work if GLFW is built as a DLL. This is disabled by default, letting 278the operating system and driver decide. 279 280 281### macOS specific CMake options {#compile_options_macos} 282 283@anchor GLFW_BUILD_COCOA 284__GLFW_BUILD_COCOA__ determines whether to include support for Cocoa when compiling the 285library. This option is only available when compiling for macOS. This is enabled by 286default. 287 288 289### Unix-like system specific CMake options {#compile_options_unix} 290 291@anchor GLFW_BUILD_WAYLAND 292__GLFW_BUILD_WAYLAND__ determines whether to include support for Wayland when compiling 293the library. This option is only available when compiling for Linux and other Unix-like 294systems other than macOS. This is enabled by default. 295 296@anchor GLFW_BUILD_X11 297__GLFW_BUILD_X11__ determines whether to include support for X11 when compiling the 298library. This option is only available when compiling for Linux and other Unix-like 299systems other than macOS. This is enabled by default. 300 301 302## Cross-compilation with CMake and MinGW {#compile_mingw_cross} 303 304Both Cygwin and many Linux distributions have MinGW or MinGW-w64 packages. For 305example, Cygwin has the `mingw64-i686-gcc` and `mingw64-x86_64-gcc` packages 306for 32- and 64-bit version of MinGW-w64, while Debian GNU/Linux and derivatives 307like Ubuntu have the `mingw-w64` package for both. 308 309GLFW has CMake toolchain files in the `CMake` subdirectory that set up 310cross-compilation of Windows binaries. To use these files you set the 311`CMAKE_TOOLCHAIN_FILE` CMake variable with the `-D` flag add an option when 312configuring and generating the build files. 313 314```sh 315cmake -S path/to/glfw -B path/to/build -D CMAKE_TOOLCHAIN_FILE=path/to/file 316``` 317 318The exact toolchain file to use depends on the prefix used by the MinGW or 319MinGW-w64 binaries on your system. You can usually see this in the /usr 320directory. For example, both the Ubuntu and Cygwin MinGW-w64 packages have 321`/usr/x86_64-w64-mingw32` for the 64-bit compilers, so the correct invocation 322would be: 323 324```sh 325cmake -S path/to/glfw -B path/to/build -D CMAKE_TOOLCHAIN_FILE=CMake/x86_64-w64-mingw32.cmake 326``` 327 328The path to the toolchain file is relative to the path to the GLFW source tree 329passed to the `-S` flag, not to the current directory. 330 331For more details see the [CMake toolchain guide][cmake-toolchains]. 332 333[cmake-toolchains]: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html 334 335 336## Compiling GLFW manually {#compile_manual} 337 338If you wish to compile GLFW without its CMake build environment then you will have to do 339at least some platform-detection yourself. There are preprocessor macros for 340enabling support for the platforms (window systems) available. There are also optional, 341platform-specific macros for various features. 342 343When building, GLFW will expect the necessary configuration macros to be defined 344on the command-line. The GLFW CMake files set these as private compile 345definitions on the GLFW target but if you compile the GLFW sources manually you 346will need to define them yourself. 347 348The window system is used to create windows, handle input, monitors, gamma ramps and 349clipboard. The options are: 350 351 - @b _GLFW_COCOA to use the Cocoa frameworks 352 - @b _GLFW_WIN32 to use the Win32 API 353 - @b _GLFW_WAYLAND to use the Wayland protocol 354 - @b _GLFW_X11 to use the X Window System 355 356The @b _GLFW_WAYLAND and @b _GLFW_X11 macros may be combined and produces a library that 357attempts to detect the appropriate platform at initialization. 358 359If you are building GLFW as a shared library / dynamic library / DLL then you 360must also define @b _GLFW_BUILD_DLL. Otherwise, you must not define it. 361 362If you are using a custom name for the Vulkan, EGL, GLX, OSMesa, OpenGL, GLESv1 363or GLESv2 library, you can override the default names by defining those you need 364of @b _GLFW_VULKAN_LIBRARY, @b _GLFW_EGL_LIBRARY, @b _GLFW_GLX_LIBRARY, @b 365_GLFW_OSMESA_LIBRARY, @b _GLFW_OPENGL_LIBRARY, @b _GLFW_GLESV1_LIBRARY and @b 366_GLFW_GLESV2_LIBRARY. Otherwise, GLFW will use the built-in default names. 367 368@note None of the @ref build_macros may be defined during the compilation of 369GLFW. If you define any of these in your build files, make sure they are not 370applied to the GLFW sources. 371 372