build.dox 14 KB

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