build.dox 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/glad.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/glad.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](http://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 linking an application under Windows that uses the static version of GLFW,
  119. you must link with `opengl32`. On some versions of MinGW, you must also
  120. explicitly link with `gdi32`, while other versions of MinGW include it in the
  121. set of default libraries along with other dependencies like `user32` and
  122. `kernel32`. 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 `opengl32` if your application
  129. uses OpenGL and `glu32` if it uses GLU.
  130. @subsection build_link_cmake_source With CMake and GLFW source
  131. This section is about using CMake to compile and link GLFW along with your
  132. application. If you want to use an installed binary instead, see @ref
  133. build_link_cmake_package.
  134. With just a few changes to your `CMakeLists.txt` you can have the GLFW source
  135. tree built along with your application.
  136. When including GLFW as part of your build, you probably don't want to build the
  137. GLFW tests, examples and documentation. To disable these, set the corresponding
  138. cache variables before adding the GLFW source tree.
  139. @code
  140. set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
  141. set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
  142. set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
  143. @endcode
  144. Then add the root directory of the GLFW source tree to your project. This
  145. will add the `glfw` target and the necessary cache variables to your project.
  146. @code{.cmake}
  147. add_subdirectory(path/to/glfw)
  148. @endcode
  149. Once GLFW has been added to the project, link against it with the `glfw` target.
  150. This adds all link-time dependencies of GLFW as it is currently configured,
  151. the include directory for the GLFW header and, when applicable, the @ref
  152. GLFW_DLL macro.
  153. @code{.cmake}
  154. target_link_libraries(myapp glfw)
  155. @endcode
  156. Note that the dependencies do not include OpenGL or GLU, as GLFW loads any
  157. OpenGL, OpenGL ES or Vulkan libraries it needs at runtime and does not use GLU.
  158. If your application calls OpenGL directly, instead of using a modern
  159. [extension loader library](@ref context_glext_auto) you can find it by requiring
  160. the OpenGL package.
  161. @code{.cmake}
  162. find_package(OpenGL REQUIRED)
  163. @endcode
  164. If OpenGL is found, the `OPENGL_FOUND` variable is true and the
  165. `OPENGL_INCLUDE_DIR` and `OPENGL_gl_LIBRARY` cache variables can be used.
  166. @code{.cmake}
  167. target_include_directories(myapp ${OPENGL_INCLUDE_DIR})
  168. target_link_libraries(myapp ${OPENGL_gl_LIBRARY})
  169. @endcode
  170. The OpenGL CMake package also looks for GLU. If GLU is found, the
  171. `OPENGL_GLU_FOUND` variable is true and the `OPENGL_INCLUDE_DIR` and
  172. `OPENGL_glu_LIBRARY` cache variables can be used.
  173. @code{.cmake}
  174. target_link_libraries(myapp ${OPENGL_glu_LIBRARY})
  175. @endcode
  176. @note GLU has been deprecated and should not be used in new code, but some
  177. legacy code requires it. See the [section on GLU](@ref moving_glu) in the
  178. transition guide for suggested replacements.
  179. @subsection build_link_cmake_package With CMake and installed GLFW binaries
  180. This section is about using CMake to link GLFW after it has been built and
  181. installed. If you want to build it along with your application instead, see
  182. @ref build_link_cmake_source.
  183. With just a few changes to your `CMakeLists.txt`, you can locate the package and
  184. target files generated when GLFW is installed.
  185. @code{.cmake}
  186. find_package(glfw3 3.3 REQUIRED)
  187. @endcode
  188. Once GLFW has been added to the project, link against it with the `glfw` target.
  189. This adds all link-time dependencies of GLFW as it is currently configured,
  190. the include directory for the GLFW header and, when applicable, the @ref
  191. GLFW_DLL macro.
  192. @code{.cmake}
  193. target_link_libraries(myapp glfw)
  194. @endcode
  195. Note that the dependencies do not include OpenGL or GLU, as GLFW loads any
  196. OpenGL, OpenGL ES or Vulkan libraries it needs at runtime and does not use GLU.
  197. If your application calls OpenGL directly, instead of using a modern
  198. [extension loader library](@ref context_glext_auto) you can find it by requiring
  199. the OpenGL package.
  200. @code{.cmake}
  201. find_package(OpenGL REQUIRED)
  202. @endcode
  203. If OpenGL is found, the `OPENGL_FOUND` variable is true and the
  204. `OPENGL_INCLUDE_DIR` and `OPENGL_gl_LIBRARY` cache variables can be used.
  205. @code{.cmake}
  206. target_include_directories(myapp ${OPENGL_INCLUDE_DIR})
  207. target_link_libraries(myapp ${OPENGL_gl_LIBRARY})
  208. @endcode
  209. The OpenGL CMake package also looks for GLU. If GLU is found, the
  210. `OPENGL_GLU_FOUND` variable is true and the `OPENGL_INCLUDE_DIR` and
  211. `OPENGL_glu_LIBRARY` cache variables can be used.
  212. @code{.cmake}
  213. target_link_libraries(myapp ${OPENGL_glu_LIBRARY})
  214. @endcode
  215. @note GLU has been deprecated and should not be used in new code, but some
  216. legacy code requires it. See the [section on GLU](@ref moving_glu) in the
  217. transition guide for suggested replacements.
  218. @subsection build_link_pkgconfig With makefiles and pkg-config on Unix
  219. GLFW supports [pkg-config](http://www.freedesktop.org/wiki/Software/pkg-config/),
  220. and the `glfw3.pc` pkg-config file is generated when the GLFW library is built
  221. and is installed along with it. A pkg-config file describes all necessary
  222. compile-time and link-time flags and dependencies needed to use a library. When
  223. they are updated or if they differ between systems, you will get the correct
  224. ones automatically.
  225. A typical compile and link command-line when using the static version of the
  226. GLFW library may look like this:
  227. @code{.sh}
  228. cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --static --libs glfw3`
  229. @endcode
  230. If you are using the shared version of the GLFW library, simply omit the
  231. `--static` flag.
  232. @code{.sh}
  233. cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`
  234. @endcode
  235. You can also use the `glfw3.pc` file without installing it first, by using the
  236. `PKG_CONFIG_PATH` environment variable.
  237. @code{.sh}
  238. env PKG_CONFIG_PATH=path/to/glfw/src cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`
  239. @endcode
  240. The dependencies do not include OpenGL or GLU, as GLFW loads any OpenGL, OpenGL
  241. ES or Vulkan libraries it needs at runtime and does not use GLU. On macOS, GLU
  242. is built into the OpenGL framework, so if you need GLU you don't need to do
  243. anything extra. If you need GLU and are using Linux or BSD, you should add the
  244. `glu` pkg-config package.
  245. @code{.sh}
  246. cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --libs glfw3 glu`
  247. @endcode
  248. @note GLU has been deprecated and should not be used in new code, but some
  249. legacy code requires it. See the [section on GLU](@ref moving_glu) in the
  250. transition guide for suggested replacements.
  251. If you are using the static version of the GLFW library, make sure you don't
  252. link statically against GLU.
  253. @code{.sh}
  254. cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --static --libs glfw3` `pkg-config --libs glu`
  255. @endcode
  256. @subsection build_link_xcode With Xcode on macOS
  257. If you are using the dynamic library version of GLFW, simply add it to the
  258. project dependencies.
  259. If you are using the static library version of GLFW, add it and the Cocoa,
  260. OpenGL, IOKit and CoreVideo frameworks to the project as dependencies. They can
  261. all be found in `/System/Library/Frameworks`.
  262. @subsection build_link_osx With command-line on macOS
  263. It is recommended that you use [pkg-config](@ref build_link_pkgconfig) when
  264. building from the command line on macOS. That way you will get any new
  265. dependencies added automatically. If you still wish to build manually, you need
  266. to add the required frameworks and libraries to your command-line yourself using
  267. the `-l` and `-framework` switches.
  268. If you are using the dynamic GLFW library, which is named `libglfw.3.dylib`, do:
  269. @code{.sh}
  270. cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
  271. @endcode
  272. If you are using the static library, named `libglfw3.a`, substitute `-lglfw3`
  273. for `-lglfw`.
  274. Note that you do not add the `.framework` extension to a framework when linking
  275. against it from the command-line.
  276. The OpenGL framework contains both the OpenGL and GLU APIs, so there is nothing
  277. special to do when using GLU. Also note that even though your machine may have
  278. `libGL`-style OpenGL libraries, they are for use with the X Window System and
  279. will _not_ work with the macOS native version of GLFW.
  280. */