compile.dox 14 KB

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