compile.dox 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*!
  2. @page compile_guide Compiling GLFW
  3. @tableofcontents
  4. This is about compiling the GLFW library itself. For information on how to
  5. build applications that use GLFW, see @ref build_guide.
  6. @section compile_cmake Using CMake
  7. @note GLFW behaves like most other libraries that use CMake so this guide mostly
  8. describes the basic configure/generate/compile sequence. If you are already
  9. familiar with this from other projects, you may want to focus on the @ref
  10. compile_deps and @ref compile_options sections for GLFW-specific information.
  11. GLFW uses [CMake](https://cmake.org/) to generate project files or makefiles
  12. for your chosen development environment. To compile GLFW, first generate these
  13. files with CMake and then use them to compile the GLFW library.
  14. If you are on Windows and macOS you can
  15. [download CMake](https://cmake.org/download/) from their site.
  16. If you are on a Unix-like system such as Linux, FreeBSD or Cygwin or have
  17. a package system like Fink, MacPorts or Homebrew, you can install its CMake
  18. package.
  19. CMake is a complex tool and this guide will only show a few of the possible ways
  20. to set up and compile GLFW. The CMake project has their own much more detailed
  21. [CMake user guide](https://cmake.org/cmake/help/latest/guide/user-interaction/)
  22. that includes everything in this guide not specific to GLFW. It may be a useful
  23. companion to this one.
  24. @subsection compile_deps Installing dependencies
  25. The C/C++ development environments in Visual Studio, Xcode and MinGW come with
  26. all necessary dependencies for compiling GLFW, but on Unix-like systems like
  27. Linux and FreeBSD you will need a few extra packages.
  28. @subsubsection compile_deps_x11 Dependencies for X11 on Unix-like systems
  29. To compile GLFW for X11, you need to have the X11 development packages
  30. installed. They are not needed to build or run programs that use GLFW.
  31. On Debian and derivatives like Ubuntu and Linux Mint the `xorg-dev` meta-package
  32. pulls in the development packages for all of X11.
  33. @code{.sh}
  34. sudo apt install xorg-dev
  35. @endcode
  36. On Fedora and derivatives like Red Hat the X11 extension packages
  37. `libXcursor-devel`, `libXi-devel`, `libXinerama-devel` and `libXrandr-devel`
  38. required by GLFW pull in all its other dependencies.
  39. @code{.sh}
  40. sudo dnf install libXcursor-devel libXi-devel libXinerama-devel libXrandr-devel
  41. @endcode
  42. On FreeBSD the X11 headers are installed along the end-user X11 packages, so if
  43. you have an X server running you should have the headers as well. If not,
  44. install the `xorgproto` package.
  45. @code{.sh}
  46. pkg install xorgproto
  47. @endcode
  48. On Cygwin the `libXcursor-devel`, `libXi-devel`, `libXinerama-devel`,
  49. `libXrandr-devel` and `libXrender-devel` packages in the Libs section of the GUI
  50. installer will install all the headers and other development related files GLFW
  51. requires for X11.
  52. Once you have the required depdendencies, move on to @ref compile_generate.
  53. @subsubsection compile_deps_wayland Dependencies for Wayland on Unix-like systems
  54. To compile GLFW for Wayland, you need to have the Wayland and xkbcommon
  55. development packages installed. They are not needed to build or run programs
  56. that use GLFW.
  57. On Debian and derivatives like Ubuntu and Linux Mint you will need the `libwayland-dev`,
  58. `libxkbcommon-dev`, `wayland-protocols` and `extra-cmake-modules` packages.
  59. These will pull in all other dependencies.
  60. @code{.sh}
  61. sudo apt install libwayland-dev libxkbcommon-dev wayland-protocols extra-cmake-modules
  62. @endcode
  63. On Fedora and derivatives like Red Hat you will need the `wayland-devel`,
  64. `libxkbcommon-devel`, `wayland-protocols-devel` and `extra-cmake-modules` packages.
  65. @code{.sh}
  66. sudo dnf install wayland-devel libxkbcommon-devel wayland-protocols-devel extra-cmake-modules
  67. @endcode
  68. On FreeBSD you will need the `wayland`, `libxkbcommon`, `wayland-protocols`,
  69. `evdev-proto` and `kf5-extra-cmake-modules` packages.
  70. @code{.sh}
  71. pkg install wayland libxkbcommon wayland-protocols evdev-proto kf5-extra-cmake-modules
  72. @endcode
  73. Once you have the required depdendencies, move on to @ref compile_generate.
  74. @subsection compile_generate Generating build files with CMake
  75. Once you have all necessary dependencies it is time to generate the project
  76. files or makefiles for your development environment. CMake needs two paths for
  77. this:
  78. - the path to the root directory of the GLFW source tree (not its `src`
  79. subdirectory)
  80. - the path to the directory where the generated build files and compiled
  81. binaries will be placed
  82. If these are the same, it is called an in-tree build, otherwise it is called an
  83. out-of-tree build.
  84. Out-of-tree builds are recommended as they avoid cluttering up the source tree.
  85. They also allow you to have several build directories for different
  86. configurations all using the same source tree.
  87. A common pattern when building a single configuration is to have a build
  88. directory named `build` in the root of the source tree.
  89. @subsubsection compile_generate_gui Generating files with the CMake GUI
  90. Start the CMake GUI and set the paths to the source and build directories
  91. described above. Then press _Configure_ and _Generate_.
  92. If you wish change any CMake variables in the list, press _Configure_ and then
  93. _Generate_ to have the new values take effect. The variable list will be
  94. populated after the first configure step.
  95. By default, GLFW will use X11 on Linux and other Unix-like systems other
  96. than macOS. To use Wayland instead, set the `GLFW_USE_WAYLAND` option in the
  97. GLFW section of the variable list, then apply the new value as described above.
  98. Once you have generated the project files or makefiles for your chosen
  99. development environment, move on to @ref compile_compile.
  100. @subsubsection compile_generate_cli Generating files with the CMake command-line tool
  101. To make a build directory, pass the source and build directories to the `cmake`
  102. command. These can be relative or absolute paths. The build directory is
  103. created if it doesn't already exist.
  104. @code{.sh}
  105. cmake -S path/to/glfw -B path/to/build
  106. @endcode
  107. It is common to name the build directory `build` and place it in the root of the
  108. source tree when only planning to build a single configuration.
  109. @code{.sh}
  110. cd path/to/glfw
  111. cmake -S . -B build
  112. @endcode
  113. Without other flags these will generate Visual Studio project files on Windows
  114. and makefiles on other platforms. You can choose other targets using the `-G`
  115. flag.
  116. @code{.sh}
  117. cmake -S path/to/glfw -B path/to/build -G Xcode
  118. @endcode
  119. By default, GLFW will use X11 on Linux and other Unix-like systems other
  120. than macOS. To use Wayland instead, set the `GLFW_USE_WAYLAND` CMake option.
  121. @code{.sh}
  122. cmake -S path/to/glfw -B path/to/build -D GLFW_USE_WAYLAND=1
  123. @endcode
  124. Once you have generated the project files or makefiles for your chosen
  125. development environment, move on to @ref compile_compile.
  126. @subsection compile_compile Compiling the library
  127. You should now have all required dependencies and the project files or makefiles
  128. necessary to compile GLFW. Go ahead and compile the actual GLFW library with
  129. these files as you would with any other project.
  130. With Visual Studio open `GLFW.sln` and use the Build menu. With Xcode open
  131. `GLFW.xcodeproj` and use the Project menu.
  132. With Linux, macOS and other forms of Unix, run `make`.
  133. @code{.sh}
  134. cd path/to/build
  135. make
  136. @endcode
  137. With MinGW, it is `mingw32-make`.
  138. @code{.sh}
  139. cd path/to/build
  140. mingw32-make
  141. @endcode
  142. Any CMake build directory can also be built with the `cmake` command and the
  143. `--build` flag.
  144. @code{.sh}
  145. cmake --build path/to/build
  146. @endcode
  147. This will run the platform specific build tool the directory was generated for.
  148. Once the GLFW library is compiled you are ready to build your application,
  149. linking it to the GLFW library. See @ref build_guide for more information.
  150. @section compile_options CMake options
  151. The CMake files for GLFW provide a number of options, although not all are
  152. available on all supported platforms. Some of these are de facto standards
  153. among projects using CMake and so have no `GLFW_` prefix.
  154. If you are using the GUI version of CMake, these are listed and can be changed
  155. from there. If you are using the command-line version of CMake you can use the
  156. `ccmake` ncurses GUI to set options. Some package systems like Ubuntu and other
  157. distributions based on Debian GNU/Linux have this tool in a separate
  158. `cmake-curses-gui` package.
  159. Finally, if you don't want to use any GUI, you can set options from the `cmake`
  160. command-line with the `-D` flag.
  161. @code{.sh}
  162. cmake -S path/to/glfw -B path/to/build -D BUILD_SHARED_LIBS=ON
  163. @endcode
  164. @subsection compile_options_shared Shared CMake options
  165. @anchor BUILD_SHARED_LIBS
  166. __BUILD_SHARED_LIBS__ determines whether GLFW is built as a static
  167. library or as a DLL / shared library / dynamic library. This is disabled by
  168. default, producing a static GLFW library.
  169. @anchor GLFW_BUILD_EXAMPLES
  170. __GLFW_BUILD_EXAMPLES__ determines whether the GLFW examples are built
  171. along with the library.
  172. @anchor GLFW_BUILD_TESTS
  173. __GLFW_BUILD_TESTS__ determines whether the GLFW test programs are
  174. built along with the library.
  175. @anchor GLFW_BUILD_DOCS
  176. __GLFW_BUILD_DOCS__ determines whether the GLFW documentation is built along
  177. with the library. This is enabled by default if
  178. [Doxygen](https://www.doxygen.nl/) is found by CMake during configuration.
  179. @anchor GLFW_VULKAN_STATIC
  180. __GLFW_VULKAN_STATIC__ determines whether to use the Vulkan loader linked
  181. directly with the application. This is disabled by default.
  182. @subsection compile_options_win32 Windows specific CMake options
  183. @anchor USE_MSVC_RUNTIME_LIBRARY_DLL
  184. __USE_MSVC_RUNTIME_LIBRARY_DLL__ determines whether to use the DLL version or the
  185. static library version of the Visual C++ runtime library. When enabled, the
  186. DLL version of the Visual C++ library is used. This is enabled by default.
  187. On CMake 3.15 and later you can set the standard CMake
  188. [CMAKE_MSVC_RUNTIME_LIBRARY](https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html)
  189. variable instead of this GLFW-specific option.
  190. @anchor GLFW_USE_HYBRID_HPG
  191. __GLFW_USE_HYBRID_HPG__ determines whether to export the `NvOptimusEnablement` and
  192. `AmdPowerXpressRequestHighPerformance` symbols, which force the use of the
  193. high-performance GPU on Nvidia Optimus and AMD PowerXpress systems. These symbols
  194. need to be exported by the EXE to be detected by the driver, so the override
  195. will not work if GLFW is built as a DLL. This is disabled by default, letting
  196. the operating system and driver decide.
  197. @subsection compile_options_wayland Wayland specific CMake options
  198. @anchor GLFW_USE_WAYLAND
  199. __GLFW_USE_WAYLAND__ determines whether to compile the library for Wayland.
  200. This option is only available on Linux and other Unix-like systems other than
  201. macOS. This is disabled by default.
  202. @section compile_mingw_cross Cross-compilation with CMake and MinGW
  203. Both Cygwin and many Linux distributions have MinGW or MinGW-w64 packages. For
  204. example, Cygwin has the `mingw64-i686-gcc` and `mingw64-x86_64-gcc` packages
  205. for 32- and 64-bit version of MinGW-w64, while Debian GNU/Linux and derivatives
  206. like Ubuntu have the `mingw-w64` package for both.
  207. GLFW has CMake toolchain files in the `CMake` subdirectory that set up
  208. cross-compilation of Windows binaries. To use these files you set the
  209. `CMAKE_TOOLCHAIN_FILE` CMake variable with the `-D` flag add an option when
  210. configuring and generating the build files.
  211. @code{.sh}
  212. cmake -S path/to/glfw -B path/to/build -D CMAKE_TOOLCHAIN_FILE=path/to/file
  213. @endcode
  214. The exact toolchain file to use depends on the prefix used by the MinGW or
  215. MinGW-w64 binaries on your system. You can usually see this in the /usr
  216. directory. For example, both the Ubuntu and Cygwin MinGW-w64 packages have
  217. `/usr/x86_64-w64-mingw32` for the 64-bit compilers, so the correct invocation
  218. would be:
  219. @code{.sh}
  220. cmake -S path/to/glfw -B path/to/build -D CMAKE_TOOLCHAIN_FILE=CMake/x86_64-w64-mingw32.cmake
  221. @endcode
  222. The path to the toolchain file is relative to the path to the GLFW source tree
  223. passed to the `-S` flag, not to the current directory.
  224. For more details see the
  225. [CMake toolchain guide](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html).
  226. @section compile_manual Compiling GLFW manually
  227. If you wish to compile GLFW without its CMake build environment then you will
  228. have to do at least some of the platform-detection yourself. GLFW needs
  229. a configuration macro to be defined in order to know what window system it is
  230. being compiled for and also has optional, platform-specific ones for various
  231. features.
  232. When building with CMake, the `glfw_config.h` configuration header is generated
  233. based on the current platform and CMake options. The GLFW CMake environment
  234. defines @b GLFW_USE_CONFIG_H, which causes this header to be included by
  235. `internal.h`. Without this macro, GLFW will expect the necessary configuration
  236. macros to be defined on the command-line.
  237. The window creation API is used to create windows, handle input, monitors, gamma
  238. ramps and clipboard. The options are:
  239. - @b _GLFW_COCOA to use the Cocoa frameworks
  240. - @b _GLFW_WIN32 to use the Win32 API
  241. - @b _GLFW_X11 to use the X Window System
  242. - @b _GLFW_WAYLAND to use the Wayland API (experimental and incomplete)
  243. - @b _GLFW_OSMESA to use the OSMesa API (headless and non-interactive)
  244. If you are building GLFW as a shared library / dynamic library / DLL then you
  245. must also define @b _GLFW_BUILD_DLL. Otherwise, you must not define it.
  246. If you are linking the Vulkan loader directly with your application then you
  247. must also define @b _GLFW_VULKAN_STATIC. Otherwise, GLFW will attempt to use the
  248. external version.
  249. If you are using a custom name for the Vulkan, EGL, GLX, OSMesa, OpenGL, GLESv1
  250. or GLESv2 library, you can override the default names by defining those you need
  251. of @b _GLFW_VULKAN_LIBRARY, @b _GLFW_EGL_LIBRARY, @b _GLFW_GLX_LIBRARY, @b
  252. _GLFW_OSMESA_LIBRARY, @b _GLFW_OPENGL_LIBRARY, @b _GLFW_GLESV1_LIBRARY and @b
  253. _GLFW_GLESV2_LIBRARY. Otherwise, GLFW will use the built-in default names.
  254. @note None of the @ref build_macros may be defined during the compilation of
  255. GLFW. If you define any of these in your build files, make sure they are not
  256. applied to the GLFW sources.
  257. */