compile.dox 14 KB

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