build.dox 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*!
  2. @page build_guide Building applications
  3. @tableofcontents
  4. This is about compiling and linking applications that use GLFW. For information on
  5. how to write such applications, start with the
  6. [introductory tutorial](@ref quick_guide). For information on how to compile
  7. the GLFW library itself, see @ref compile_guide.
  8. This is not a tutorial on compilation or linking. It assumes basic
  9. understanding of how to compile and link a C program as well as how to use the
  10. specific compiler of your chosen development environment. The compilation
  11. and linking process should be explained in your C programming material and in
  12. the documentation for your development environment.
  13. @section build_include Including the GLFW header file
  14. You should include the GLFW header in the source files where you use OpenGL or
  15. GLFW.
  16. @code
  17. #include <GLFW/glfw3.h>
  18. @endcode
  19. This header defines all the constants and declares all the types and function
  20. prototypes of the GLFW API. By default it also includes the OpenGL header from
  21. your development environment. See [option macros](@ref build_macros) below for
  22. how to select OpenGL ES headers and more.
  23. The GLFW header also defines any platform-specific macros needed by your OpenGL
  24. header, so that it can be included without needing any window system headers.
  25. It does this only when needed, so if window system headers are included, the
  26. GLFW header does not try to redefine those symbols. The reverse is not true,
  27. i.e. `windows.h` cannot cope if any Win32 symbols have already been defined.
  28. In other words:
  29. - Use the GLFW header to include OpenGL or OpenGL ES headers portably
  30. - Do not include window system headers unless you will use those APIs directly
  31. - If you do need such headers, include them before the GLFW header
  32. If you are using an OpenGL extension loading library such as
  33. [glad](https://github.com/Dav1dde/glad), the extension loader header should
  34. be included before the GLFW one. GLFW attempts to detect any OpenGL or OpenGL
  35. ES header or extension loader header included before it and will then disable
  36. the inclusion of the default OpenGL header. Most extension loaders also define
  37. macros that disable similar headers below it.
  38. @code
  39. #include <glad/gl.h>
  40. #include <GLFW/glfw3.h>
  41. @endcode
  42. Both of these mechanisms depend on the extension loader header defining a known
  43. macro. If yours doesn't or you don't know which one your users will pick, the
  44. @ref GLFW_INCLUDE_NONE macro will explicitly to prevent the GLFW header from
  45. including the OpenGL header. This will also allow you to include the two
  46. headers in any order.
  47. @code
  48. #define GLFW_INCLUDE_NONE
  49. #include <GLFW/glfw3.h>
  50. #include <glad/gl.h>
  51. @endcode
  52. @subsection build_macros GLFW header option macros
  53. These macros may be defined before the inclusion of the GLFW header and affect
  54. its behavior.
  55. @anchor GLFW_DLL
  56. __GLFW_DLL__ is required on Windows when using the GLFW DLL, to tell the
  57. compiler that the GLFW functions are defined in a DLL.
  58. The following macros control which OpenGL or OpenGL ES API header is included.
  59. Only one of these may be defined at a time.
  60. @note GLFW does not provide any of the API headers mentioned below. They are
  61. provided by your development environment or your OpenGL, OpenGL ES or Vulkan
  62. SDK, and most of them can be downloaded from the
  63. [Khronos Registry](https://www.khronos.org/registry/).
  64. @anchor GLFW_INCLUDE_GLCOREARB
  65. __GLFW_INCLUDE_GLCOREARB__ makes the GLFW header include the modern
  66. `GL/glcorearb.h` header (`OpenGL/gl3.h` on macOS) instead of the regular OpenGL
  67. header.
  68. @anchor GLFW_INCLUDE_ES1
  69. __GLFW_INCLUDE_ES1__ makes the GLFW header include the OpenGL ES 1.x `GLES/gl.h`
  70. header instead of the regular OpenGL header.
  71. @anchor GLFW_INCLUDE_ES2
  72. __GLFW_INCLUDE_ES2__ makes the GLFW header include the OpenGL ES 2.0
  73. `GLES2/gl2.h` header instead of the regular OpenGL header.
  74. @anchor GLFW_INCLUDE_ES3
  75. __GLFW_INCLUDE_ES3__ makes the GLFW header include the OpenGL ES 3.0
  76. `GLES3/gl3.h` header instead of the regular OpenGL header.
  77. @anchor GLFW_INCLUDE_ES31
  78. __GLFW_INCLUDE_ES31__ makes the GLFW header include the OpenGL ES 3.1
  79. `GLES3/gl31.h` header instead of the regular OpenGL header.
  80. @anchor GLFW_INCLUDE_ES32
  81. __GLFW_INCLUDE_ES32__ makes the GLFW header include the OpenGL ES 3.2
  82. `GLES3/gl32.h` header instead of the regular OpenGL header.
  83. @anchor GLFW_INCLUDE_NONE
  84. __GLFW_INCLUDE_NONE__ makes the GLFW header not include any OpenGL or OpenGL ES
  85. API header. This is useful in combination with an extension loading library.
  86. If none of the above inclusion macros are defined, the standard OpenGL `GL/gl.h`
  87. header (`OpenGL/gl.h` on macOS) is included, unless GLFW detects the inclusion
  88. guards of any OpenGL, OpenGL ES or extension loader header it knows about.
  89. The following macros control the inclusion of additional API headers. Any
  90. number of these may be defined simultaneously, and/or together with one of the
  91. above macros.
  92. @anchor GLFW_INCLUDE_VULKAN
  93. __GLFW_INCLUDE_VULKAN__ makes the GLFW header include the Vulkan
  94. `vulkan/vulkan.h` header in addition to any selected OpenGL or OpenGL ES header.
  95. @anchor GLFW_INCLUDE_GLEXT
  96. __GLFW_INCLUDE_GLEXT__ makes the GLFW header include the appropriate extension
  97. header for the OpenGL or OpenGL ES header selected above after and in addition
  98. to that header.
  99. @anchor GLFW_INCLUDE_GLU
  100. __GLFW_INCLUDE_GLU__ makes the header include the GLU header in addition to the
  101. header selected above. This should only be used with the standard OpenGL header
  102. and only for compatibility with legacy code. GLU has been deprecated and should
  103. not be used in new code.
  104. @note None of these macros may be defined during the compilation of GLFW itself.
  105. If your build includes GLFW and you define any these in your build files, make
  106. sure they are not applied to the GLFW sources.
  107. @section build_link Link with the right libraries
  108. GLFW is essentially a wrapper of various platform-specific APIs and therefore
  109. needs to link against many different system libraries. If you are using GLFW as
  110. a shared library / dynamic library / DLL then it takes care of these links.
  111. However, if you are using GLFW as a static library then your executable will
  112. need to link against these libraries.
  113. On Windows and macOS, the list of system libraries is static and can be
  114. hard-coded into your build environment. See the section for your development
  115. environment below. On Linux and other Unix-like operating systems, the list
  116. varies but can be retrieved in various ways as described below.
  117. A good general introduction to linking is
  118. [Beginner's Guide to Linkers](https://www.lurklurk.org/linkers/linkers.html) by
  119. David Drysdale.
  120. @subsection build_link_win32 With MinGW or Visual C++ on Windows
  121. The static version of the GLFW library is named `glfw3`. When using this
  122. version, it is also necessary to link with some libraries that GLFW uses.
  123. When using MinGW to link an application with the static version of GLFW, you
  124. must also explicitly link with `gdi32`. Other toolchains including MinGW-w64
  125. include it in the set of default libraries along with other dependencies like
  126. `user32` and `kernel32`.
  127. The link library for the GLFW DLL is named `glfw3dll`. When compiling an
  128. application that uses the DLL version of GLFW, you need to define the @ref
  129. GLFW_DLL macro _before_ any inclusion of the GLFW header. This can be done
  130. either with a compiler switch or by defining it in your source code.
  131. @subsection build_link_cmake_source With CMake and GLFW source
  132. This section is about using CMake to compile and link GLFW along with your
  133. application. If you want to use an installed binary instead, see @ref
  134. build_link_cmake_package.
  135. With a few changes to your `CMakeLists.txt` you can have the GLFW source tree
  136. built along with your application.
  137. When including GLFW as part of your build, you probably don't want to build the
  138. GLFW tests, examples and documentation. To disable these, set the corresponding
  139. cache variables before adding the GLFW source tree.
  140. @code
  141. set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
  142. set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
  143. set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
  144. @endcode
  145. Add the root directory of the GLFW source tree to your project. This will add
  146. the `glfw` target to your project.
  147. @code{.cmake}
  148. add_subdirectory(path/to/glfw)
  149. @endcode
  150. Once GLFW has been added, link your application against the `glfw` target.
  151. This adds the GLFW library and its link-time dependencies as it is currently
  152. configured, the include directory for the GLFW header and, when applicable, the
  153. @ref GLFW_DLL macro.
  154. @code{.cmake}
  155. target_link_libraries(myapp glfw)
  156. @endcode
  157. Note that the `glfw` target does not depend on OpenGL, as GLFW loads any OpenGL,
  158. OpenGL ES or Vulkan libraries it needs at runtime. If your application calls
  159. OpenGL directly, instead of using a modern
  160. [extension loader library](@ref context_glext_auto), use the OpenGL CMake
  161. package.
  162. @code{.cmake}
  163. find_package(OpenGL REQUIRED)
  164. @endcode
  165. If OpenGL is found, the `OpenGL::GL` target is added to your project, containing
  166. library and include directory paths. Link against this like any other library.
  167. @code{.cmake}
  168. target_link_libraries(myapp OpenGL::GL)
  169. @endcode
  170. For a minimal example of a program and GLFW sources built with CMake, see the
  171. [GLFW CMake Starter](https://github.com/juliettef/GLFW-CMake-starter) on GitHub.
  172. @subsection build_link_cmake_package With CMake and installed GLFW binaries
  173. This section is about using CMake to link GLFW after it has been built and
  174. installed. If you want to build it along with your application instead, see
  175. @ref build_link_cmake_source.
  176. With a few changes to your `CMakeLists.txt` you can locate the package and
  177. target files generated when GLFW is installed.
  178. @code{.cmake}
  179. find_package(glfw3 3.3 REQUIRED)
  180. @endcode
  181. Once GLFW has been added to the project, link against it with the `glfw` target.
  182. This adds the GLFW library and its link-time dependencies, the include directory
  183. for the GLFW header and, when applicable, the @ref GLFW_DLL macro.
  184. @code{.cmake}
  185. target_link_libraries(myapp glfw)
  186. @endcode
  187. Note that the `glfw` target does not depend on OpenGL, as GLFW loads any OpenGL,
  188. OpenGL ES or Vulkan libraries it needs at runtime. If your application calls
  189. OpenGL directly, instead of using a modern
  190. [extension loader library](@ref context_glext_auto), use the OpenGL CMake
  191. package.
  192. @code{.cmake}
  193. find_package(OpenGL REQUIRED)
  194. @endcode
  195. If OpenGL is found, the `OpenGL::GL` target is added to your project, containing
  196. library and include directory paths. Link against this like any other library.
  197. @code{.cmake}
  198. target_link_libraries(myapp OpenGL::GL)
  199. @endcode
  200. @subsection build_link_pkgconfig With makefiles and pkg-config on Unix
  201. GLFW supports [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/),
  202. and the `glfw3.pc` pkg-config file is generated when the GLFW library is built
  203. and is installed along with it. A pkg-config file describes all necessary
  204. compile-time and link-time flags and dependencies needed to use a library. When
  205. they are updated or if they differ between systems, you will get the correct
  206. ones automatically.
  207. A typical compile and link command-line when using the static version of the
  208. GLFW library may look like this:
  209. @code{.sh}
  210. cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --static --libs glfw3)
  211. @endcode
  212. If you are using the shared version of the GLFW library, omit the `--static`
  213. flag.
  214. @code{.sh}
  215. cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --libs glfw3)
  216. @endcode
  217. You can also use the `glfw3.pc` file without installing it first, by using the
  218. `PKG_CONFIG_PATH` environment variable.
  219. @code{.sh}
  220. env PKG_CONFIG_PATH=path/to/glfw/src cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --libs glfw3)
  221. @endcode
  222. The dependencies do not include OpenGL, as GLFW loads any OpenGL, OpenGL ES or
  223. Vulkan libraries it needs at runtime. If your application calls OpenGL
  224. directly, instead of using a modern
  225. [extension loader library](@ref context_glext_auto), you should add the `gl`
  226. pkg-config package.
  227. @code{.sh}
  228. cc $(pkg-config --cflags glfw3 gl) -o myprog myprog.c $(pkg-config --libs glfw3 gl)
  229. @endcode
  230. @subsection build_link_xcode With Xcode on macOS
  231. If you are using the dynamic library version of GLFW, add it to the project
  232. dependencies.
  233. If you are using the static library version of GLFW, add it and the Cocoa,
  234. OpenGL and IOKit frameworks to the project as dependencies. They can all be
  235. found in `/System/Library/Frameworks`.
  236. @subsection build_link_osx With command-line on macOS
  237. It is recommended that you use [pkg-config](@ref build_link_pkgconfig) when
  238. building from the command line on macOS. That way you will get any new
  239. dependencies added automatically. If you still wish to build manually, you need
  240. to add the required frameworks and libraries to your command-line yourself using
  241. the `-l` and `-framework` switches.
  242. If you are using the dynamic GLFW library, which is named `libglfw.3.dylib`, do:
  243. @code{.sh}
  244. cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit
  245. @endcode
  246. If you are using the static library, named `libglfw3.a`, substitute `-lglfw3`
  247. for `-lglfw`.
  248. Note that you do not add the `.framework` extension to a framework when linking
  249. against it from the command-line.
  250. @note Your machine may have `libGL.*.dylib` style OpenGL library, but that is
  251. for the X Window System and will not work with the macOS native version of GLFW.
  252. */