build.dox 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 declares the GLFW API and by default also includes the OpenGL header
  20. from your development environment. See below for how to control this.
  21. The GLFW header also defines any platform-specific macros needed by your OpenGL
  22. header, so it can be included without needing any window system headers.
  23. For example, under Windows you are normally required to include `windows.h`
  24. before the OpenGL header, which would bring in the whole Win32 API. The GLFW
  25. header duplicates the small number of macros needed.
  26. It does this only when needed, so if `windows.h` _is_ included, the GLFW header
  27. does not try to redefine those symbols. The reverse is not true, i.e.
  28. `windows.h` cannot cope if any of its symbols have already been defined.
  29. In other words:
  30. - Do _not_ include the OpenGL headers yourself, as GLFW does this for you
  31. - Do _not_ include `windows.h` or other platform-specific headers unless you
  32. plan on using those APIs directly
  33. - If you _do_ need to include such headers, do it _before_ including
  34. the GLFW header and it will handle this
  35. If you are using an OpenGL extension loading library such as
  36. [glad](https://github.com/Dav1dde/glad), the extension loader header should
  37. be included _before_ the GLFW one.
  38. @code
  39. #include <glad/gl.h>
  40. #include <GLFW/glfw3.h>
  41. @endcode
  42. Alternatively the @ref GLFW_INCLUDE_NONE macro (described below) can be used to
  43. prevent the GLFW header from including the OpenGL header.
  44. @code
  45. #define GLFW_INCLUDE_NONE
  46. #include <GLFW/glfw3.h>
  47. #include <glad/gl.h>
  48. @endcode
  49. @subsection build_macros GLFW header option macros
  50. These macros may be defined before the inclusion of the GLFW header and affect
  51. its behavior.
  52. @anchor GLFW_DLL
  53. __GLFW_DLL__ is required on Windows when using the GLFW DLL, to tell the
  54. compiler that the GLFW functions are defined in a DLL.
  55. The following macros control which OpenGL or OpenGL ES API header is included.
  56. Only one of these may be defined at a time.
  57. @anchor GLFW_INCLUDE_GLCOREARB
  58. __GLFW_INCLUDE_GLCOREARB__ makes the GLFW header include the modern
  59. `GL/glcorearb.h` header (`OpenGL/gl3.h` on macOS) instead of the regular OpenGL
  60. header.
  61. @anchor GLFW_INCLUDE_ES1
  62. __GLFW_INCLUDE_ES1__ makes the GLFW header include the OpenGL ES 1.x `GLES/gl.h`
  63. header instead of the regular OpenGL header.
  64. @anchor GLFW_INCLUDE_ES2
  65. __GLFW_INCLUDE_ES2__ makes the GLFW header include the OpenGL ES 2.0
  66. `GLES2/gl2.h` header instead of the regular OpenGL header.
  67. @anchor GLFW_INCLUDE_ES3
  68. __GLFW_INCLUDE_ES3__ makes the GLFW header include the OpenGL ES 3.0
  69. `GLES3/gl3.h` header instead of the regular OpenGL header.
  70. @anchor GLFW_INCLUDE_ES31
  71. __GLFW_INCLUDE_ES31__ makes the GLFW header include the OpenGL ES 3.1
  72. `GLES3/gl31.h` header instead of the regular OpenGL header.
  73. @anchor GLFW_INCLUDE_ES32
  74. __GLFW_INCLUDE_ES31__ makes the GLFW header include the OpenGL ES 3.2
  75. `GLES3/gl32.h` header instead of the regular OpenGL header.
  76. @anchor GLFW_INCLUDE_NONE
  77. __GLFW_INCLUDE_NONE__ makes the GLFW header not include any OpenGL or OpenGL ES
  78. API header. This is useful in combination with an extension loading library.
  79. If none of the above inclusion macros are defined, the standard OpenGL `GL/gl.h`
  80. header (`OpenGL/gl.h` on macOS) is included.
  81. The following macros control the inclusion of additional API headers. Any
  82. number of these may be defined simultaneously, and/or together with one of the
  83. above macros.
  84. @anchor GLFW_INCLUDE_VULKAN
  85. __GLFW_INCLUDE_VULKAN__ makes the GLFW header include the Vulkan
  86. `vulkan/vulkan.h` header in addition to any selected OpenGL or OpenGL ES header.
  87. @anchor GLFW_INCLUDE_GLEXT
  88. __GLFW_INCLUDE_GLEXT__ makes the GLFW header include the appropriate extension
  89. header for the OpenGL or OpenGL ES header selected above after and in addition
  90. to that header.
  91. @anchor GLFW_INCLUDE_GLU
  92. __GLFW_INCLUDE_GLU__ makes the header include the GLU header in addition to the
  93. header selected above. This should only be used with the standard OpenGL header
  94. and only for compatibility with legacy code. GLU has been deprecated and should
  95. not be used in new code.
  96. @note GLFW does not provide any of the API headers mentioned above. They must
  97. be provided by your development environment or your OpenGL, OpenGL ES or Vulkan
  98. SDK.
  99. @note None of these macros may be defined during the compilation of GLFW itself.
  100. If your build includes GLFW and you define any these in your build files, make
  101. sure they are not applied to the GLFW sources.
  102. @section build_link Link with the right libraries
  103. GLFW is essentially a wrapper of various platform-specific APIs and therefore
  104. needs to link against many different system libraries. If you are using GLFW as
  105. a shared library / dynamic library / DLL then it takes care of these links.
  106. However, if you are using GLFW as a static library then your executable will
  107. need to link against these libraries.
  108. On Windows and macOS, the list of system libraries is static and can be
  109. hard-coded into your build environment. See the section for your development
  110. environment below. On Linux and other Unix-like operating systems, the list
  111. varies but can be retrieved in various ways as described below.
  112. A good general introduction to linking is
  113. [Beginner's Guide to Linkers](https://www.lurklurk.org/linkers/linkers.html) by
  114. David Drysdale.
  115. @subsection build_link_win32 With MinGW or Visual C++ on Windows
  116. The static version of the GLFW library is named `glfw3`. When using this
  117. version, it is also necessary to link with some libraries that GLFW uses.
  118. When using MinGW to link an application with the static version of GLFW, you
  119. must also explicitly link with `gdi32`. Other toolchains including MinGW-w64
  120. include it in the set of default libraries along with other dependencies like
  121. `user32` and `kernel32`.
  122. If you are using GLU, you must also link with `glu32`.
  123. The link library for the GLFW DLL is named `glfw3dll`. When compiling an
  124. application that uses the DLL version of GLFW, you need to define the @ref
  125. GLFW_DLL macro _before_ any inclusion of the GLFW header. This can be done
  126. either with a compiler switch or by defining it in your source code.
  127. An application using the GLFW DLL does not need to link against any of its
  128. dependencies, but you still have to link against `glu32` if it uses GLU.
  129. @subsection build_link_cmake_source With CMake and GLFW source
  130. This section is about using CMake to compile and link GLFW along with your
  131. application. If you want to use an installed binary instead, see @ref
  132. build_link_cmake_package.
  133. With a few changes to your `CMakeLists.txt` you can have the GLFW source tree
  134. built along with your application.
  135. Add the root directory of the GLFW source tree to your project. This will add
  136. the `glfw` target and the necessary cache variables to your project.
  137. @code{.cmake}
  138. add_subdirectory(path/to/glfw)
  139. @endcode
  140. Once GLFW has been added to the project, link against it with the `glfw` target.
  141. This adds all link-time dependencies of GLFW as it is currently configured,
  142. the include directory for the GLFW header and, when applicable, the @ref
  143. GLFW_DLL macro.
  144. @code{.cmake}
  145. target_link_libraries(myapp glfw)
  146. @endcode
  147. Note that the dependencies do not include OpenGL or GLU, as GLFW loads any
  148. OpenGL, OpenGL ES or Vulkan libraries it needs at runtime and does not use GLU.
  149. If your application calls OpenGL directly, instead of using a modern
  150. [extension loader library](@ref context_glext_auto) you can find it by requiring
  151. the OpenGL package.
  152. @code{.cmake}
  153. find_package(OpenGL REQUIRED)
  154. @endcode
  155. If OpenGL is found, the `OPENGL_FOUND` variable is true and the
  156. `OPENGL_INCLUDE_DIR` and `OPENGL_gl_LIBRARY` cache variables can be used.
  157. @code{.cmake}
  158. target_include_directories(myapp PUBLIC ${OPENGL_INCLUDE_DIR})
  159. target_link_libraries(myapp ${OPENGL_gl_LIBRARY})
  160. @endcode
  161. The OpenGL CMake package also looks for GLU. If GLU is found, the
  162. `OPENGL_GLU_FOUND` variable is true and the `OPENGL_INCLUDE_DIR` and
  163. `OPENGL_glu_LIBRARY` cache variables can be used.
  164. @code{.cmake}
  165. target_link_libraries(myapp ${OPENGL_glu_LIBRARY})
  166. @endcode
  167. @note GLU has been deprecated and should not be used in new code, but some
  168. legacy code requires it. See the [section on GLU](@ref moving_glu) in the
  169. transition guide for suggested replacements.
  170. @subsection build_link_cmake_package With CMake and installed GLFW binaries
  171. This section is about using CMake to link GLFW after it has been built and
  172. installed. If you want to build it along with your application instead, see
  173. @ref build_link_cmake_source.
  174. With a few changes to your `CMakeLists.txt` you can locate the package and
  175. target files generated when GLFW is installed.
  176. @code{.cmake}
  177. find_package(glfw3 3.4 REQUIRED)
  178. @endcode
  179. Once GLFW has been added to the project, link against it with the `glfw` target.
  180. This adds all link-time dependencies of GLFW as it is currently configured,
  181. the include directory for the GLFW header and, when applicable, the @ref
  182. GLFW_DLL macro.
  183. @code{.cmake}
  184. target_link_libraries(myapp glfw)
  185. @endcode
  186. Note that the dependencies do not include OpenGL or GLU, as GLFW loads any
  187. OpenGL, OpenGL ES or Vulkan libraries it needs at runtime and does not use GLU.
  188. If your application calls OpenGL directly, instead of using a modern
  189. [extension loader library](@ref context_glext_auto) you can find it by requiring
  190. the OpenGL package.
  191. @code{.cmake}
  192. find_package(OpenGL REQUIRED)
  193. @endcode
  194. If OpenGL is found, the `OPENGL_FOUND` variable is true and the
  195. `OPENGL_INCLUDE_DIR` and `OPENGL_gl_LIBRARY` cache variables can be used.
  196. @code{.cmake}
  197. target_include_directories(myapp PUBLIC ${OPENGL_INCLUDE_DIR})
  198. target_link_libraries(myapp ${OPENGL_gl_LIBRARY})
  199. @endcode
  200. The OpenGL CMake package also looks for GLU. If GLU is found, the
  201. `OPENGL_GLU_FOUND` variable is true and the `OPENGL_INCLUDE_DIR` and
  202. `OPENGL_glu_LIBRARY` cache variables can be used.
  203. @code{.cmake}
  204. target_link_libraries(myapp ${OPENGL_glu_LIBRARY})
  205. @endcode
  206. @note GLU has been deprecated and should not be used in new code, but some
  207. legacy code requires it. See the [section on GLU](@ref moving_glu) in the
  208. transition guide for suggested replacements.
  209. @subsection build_link_pkgconfig With makefiles and pkg-config on Unix
  210. GLFW supports [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/),
  211. and the `glfw3.pc` pkg-config file is generated when the GLFW library is built
  212. and is installed along with it. A pkg-config file describes all necessary
  213. compile-time and link-time flags and dependencies needed to use a library. When
  214. they are updated or if they differ between systems, you will get the correct
  215. ones automatically.
  216. A typical compile and link command-line when using the static version of the
  217. GLFW library may look like this:
  218. @code{.sh}
  219. cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --static --libs glfw3`
  220. @endcode
  221. If you are using the shared version of the GLFW library, omit the `--static`
  222. flag.
  223. @code{.sh}
  224. cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`
  225. @endcode
  226. You can also use the `glfw3.pc` file without installing it first, by using the
  227. `PKG_CONFIG_PATH` environment variable.
  228. @code{.sh}
  229. env PKG_CONFIG_PATH=path/to/glfw/src cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`
  230. @endcode
  231. The dependencies do not include OpenGL or GLU, as GLFW loads any OpenGL, OpenGL
  232. ES or Vulkan libraries it needs at runtime and does not use GLU. On macOS, GLU
  233. is built into the OpenGL framework, so if you need GLU you don't need to do
  234. anything extra. If you need GLU and are using Linux or BSD, you should add the
  235. `glu` pkg-config package.
  236. @code{.sh}
  237. cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --libs glfw3 glu`
  238. @endcode
  239. @note GLU has been deprecated and should not be used in new code, but some
  240. legacy code requires it. See the [section on GLU](@ref moving_glu) in the
  241. transition guide for suggested replacements.
  242. If you are using the static version of the GLFW library, make sure you don't
  243. link statically against GLU.
  244. @code{.sh}
  245. cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --static --libs glfw3` `pkg-config --libs glu`
  246. @endcode
  247. @subsection build_link_xcode With Xcode on macOS
  248. If you are using the dynamic library version of GLFW, add it to the project
  249. dependencies.
  250. If you are using the static library version of GLFW, add it and the Cocoa,
  251. OpenGL, IOKit and CoreVideo frameworks to the project as dependencies. They can
  252. all be found in `/System/Library/Frameworks`.
  253. @subsection build_link_osx With command-line on macOS
  254. It is recommended that you use [pkg-config](@ref build_link_pkgconfig) when
  255. building from the command line on macOS. That way you will get any new
  256. dependencies added automatically. If you still wish to build manually, you need
  257. to add the required frameworks and libraries to your command-line yourself using
  258. the `-l` and `-framework` switches.
  259. If you are using the dynamic GLFW library, which is named `libglfw.3.dylib`, do:
  260. @code{.sh}
  261. cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
  262. @endcode
  263. If you are using the static library, named `libglfw3.a`, substitute `-lglfw3`
  264. for `-lglfw`.
  265. Note that you do not add the `.framework` extension to a framework when linking
  266. against it from the command-line.
  267. The OpenGL framework contains both the OpenGL and GLU APIs, so there is nothing
  268. special to do when using GLU. Also note that even though your machine may have
  269. `libGL`-style OpenGL libraries, they are for use with the X Window System and
  270. will _not_ work with the macOS native version of GLFW.
  271. */