build.dox 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 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 Visual C++ and GLFW binaries
  121. If you are using a downloaded
  122. [binary archive](https://www.glfw.org/download.html), first make sure you have
  123. the archive matching the architecture you are building for (32-bit or 64-bit),
  124. or you will get link errors. Also make sure you are using the binaries for your
  125. version of Visual C++ or you may get other link errors.
  126. There are two version of the static GLFW library in the binary archive, because
  127. it needs to use the same base run-time library variant as the rest of your
  128. executable.
  129. One is named `glfw3.lib` and is for projects with the _Runtime Library_ project
  130. option set to _Multi-threaded DLL_ or _Multi-threaded Debug DLL_. The other is
  131. named `glfw3_mt.lib` and is for projects with _Runtime Library_ set to
  132. _Multi-threaded_ or _Multi-threaded Debug_. To use the static GLFW library you
  133. will need to add `path/to/glfw3.lib` or `path/to/glfw3_mt.lib` to the
  134. _Additional Dependencies_ project option.
  135. If you compiled a GLFW static library yourself then there will only be one,
  136. named `glfw3.lib`, and you have to make sure the run-time library variant
  137. matches.
  138. The DLL version of the GLFW library is named `glfw3.dll`, but you will be
  139. linking against the `glfw3dll.lib` link library. To use the DLL you will need
  140. to add `path/to/glfw3dll.lib` to the _Additional Dependencies_ project option.
  141. All of its dependencies are already listed there by default, but when building
  142. with the DLL version of GLFW, you also need to define the @ref GLFW_DLL. This
  143. can be done either in the _Preprocessor Definitions_ project option or by
  144. defining it in your source code before including the GLFW header.
  145. @code
  146. #define GLFW_DLL
  147. #include <GLFW/glfw3.h>
  148. @endcode
  149. All link-time dependencies for GLFW are already listed in the _Additional
  150. Dependencies_ option by default.
  151. @subsection build_link_mingw With MinGW-w64 and GLFW binaries
  152. This is intended for building a program from the command-line or by writing
  153. a makefile, on Windows with [MinGW-w64](https://www.mingw-w64.org/) and GLFW
  154. binaries. These can be from a downloaded and extracted
  155. [binary archive](https://www.glfw.org/download.html) or by compiling GLFW
  156. yourself. The paths below assume a binary archive is used.
  157. If you are using a downloaded binary archive, first make sure you have the
  158. archive matching the architecture you are building for (32-bit or 64-bit) or you
  159. will get link errors.
  160. Note that the order of source files and libraries matter for GCC. Dependencies
  161. must be listed after the files that depend on them. Any source files that
  162. depend on GLFW must be listed before the GLFW library. GLFW in turn depends on
  163. `gdi32` and must be listed before it.
  164. If you are using the static version of the GLFW library, which is named
  165. `libglfw3.a`, do:
  166. @code{.sh}
  167. gcc -o myprog myprog.c -I path/to/glfw/include path/to/glfw/lib-mingw-w64/libglfw3.a -lgdi32
  168. @endcode
  169. If you are using the DLL version of the GLFW library, which is named
  170. `glfw3.dll`, you will need to use the `libglfw3dll.a` link library.
  171. @code{.sh}
  172. gcc -o myprog myprog.c -I path/to/glfw/include path/to/glfw/lib-mingw-w64/libglfw3dll.a -lgdi32
  173. @endcode
  174. The resulting executable will need to find `glfw3.dll` to run, typically by
  175. keeping both files in the same directory.
  176. When you are building with the DLL version of GLFW, you will also need to define
  177. the @ref GLFW_DLL macro. This can be done in your source files, as long as it
  178. done before including the GLFW header:
  179. @code
  180. #define GLFW_DLL
  181. #include <GLFW/glfw3.h>
  182. @endcode
  183. It can also be done on the command-line:
  184. @code{.sh}
  185. gcc -o myprog myprog.c -D GLFW_DLL -I path/to/glfw/include path/to/glfw/lib-mingw-w64/libglfw3dll.a -lgdi32
  186. @endcode
  187. @subsection build_link_cmake_source With CMake and GLFW source
  188. This section is about using CMake to compile and link GLFW along with your
  189. application. If you want to use an installed binary instead, see @ref
  190. build_link_cmake_package.
  191. With a few changes to your `CMakeLists.txt` you can have the GLFW source tree
  192. built along with your application.
  193. When including GLFW as part of your build, you probably don't want to build the
  194. GLFW tests, examples and documentation. To disable these, set the corresponding
  195. cache variables before adding the GLFW source tree.
  196. @code
  197. set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
  198. set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
  199. set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
  200. @endcode
  201. Add the root directory of the GLFW source tree to your project. This will add
  202. the `glfw` target to your project.
  203. @code{.cmake}
  204. add_subdirectory(path/to/glfw)
  205. @endcode
  206. Once GLFW has been added, link your application against the `glfw` target.
  207. This adds the GLFW library and its link-time dependencies as it is currently
  208. configured, the include directory for the GLFW header and, when applicable, the
  209. @ref GLFW_DLL macro.
  210. @code{.cmake}
  211. target_link_libraries(myapp glfw)
  212. @endcode
  213. Note that the `glfw` target does not depend on OpenGL, as GLFW loads any OpenGL,
  214. OpenGL ES or Vulkan libraries it needs at runtime. If your application calls
  215. OpenGL directly, instead of using a modern
  216. [extension loader library](@ref context_glext_auto), use the OpenGL CMake
  217. package.
  218. @code{.cmake}
  219. find_package(OpenGL REQUIRED)
  220. @endcode
  221. If OpenGL is found, the `OpenGL::GL` target is added to your project, containing
  222. library and include directory paths. Link against this like any other library.
  223. @code{.cmake}
  224. target_link_libraries(myapp OpenGL::GL)
  225. @endcode
  226. For a minimal example of a program and GLFW sources built with CMake, see the
  227. [GLFW CMake Starter](https://github.com/juliettef/GLFW-CMake-starter) on GitHub.
  228. @subsection build_link_cmake_package With CMake and installed GLFW binaries
  229. This section is about using CMake to link GLFW after it has been built and
  230. installed. If you want to build it along with your application instead, see
  231. @ref build_link_cmake_source.
  232. With a few changes to your `CMakeLists.txt` you can locate the package and
  233. target files generated when GLFW is installed.
  234. @code{.cmake}
  235. find_package(glfw3 3.3 REQUIRED)
  236. @endcode
  237. Once GLFW has been added to the project, link against it with the `glfw` target.
  238. This adds the GLFW library and its link-time dependencies, the include directory
  239. for the GLFW header and, when applicable, the @ref GLFW_DLL macro.
  240. @code{.cmake}
  241. target_link_libraries(myapp glfw)
  242. @endcode
  243. Note that the `glfw` target does not depend on OpenGL, as GLFW loads any OpenGL,
  244. OpenGL ES or Vulkan libraries it needs at runtime. If your application calls
  245. OpenGL directly, instead of using a modern
  246. [extension loader library](@ref context_glext_auto), use the OpenGL CMake
  247. package.
  248. @code{.cmake}
  249. find_package(OpenGL REQUIRED)
  250. @endcode
  251. If OpenGL is found, the `OpenGL::GL` target is added to your project, containing
  252. library and include directory paths. Link against this like any other library.
  253. @code{.cmake}
  254. target_link_libraries(myapp OpenGL::GL)
  255. @endcode
  256. @subsection build_link_pkgconfig With pkg-config and GLFW binaries on Unix
  257. This is intended for building a program from the command-line or by writing
  258. a makefile, on macOS or any Unix-like system like Linux, FreeBSD and Cygwin.
  259. GLFW supports [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/),
  260. and the `glfw3.pc` pkg-config file is generated when the GLFW library is built
  261. and is installed along with it. A pkg-config file describes all necessary
  262. compile-time and link-time flags and dependencies needed to use a library. When
  263. they are updated or if they differ between systems, you will get the correct
  264. ones automatically.
  265. A typical compile and link command-line when using the static version of the
  266. GLFW library may look like this:
  267. @code{.sh}
  268. cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --static --libs glfw3)
  269. @endcode
  270. If you are using the shared version of the GLFW library, omit the `--static`
  271. flag.
  272. @code{.sh}
  273. cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --libs glfw3)
  274. @endcode
  275. You can also use the `glfw3.pc` file without installing it first, by using the
  276. `PKG_CONFIG_PATH` environment variable.
  277. @code{.sh}
  278. env PKG_CONFIG_PATH=path/to/glfw/src cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --libs glfw3)
  279. @endcode
  280. The dependencies do not include OpenGL, as GLFW loads any OpenGL, OpenGL ES or
  281. Vulkan libraries it needs at runtime. If your application calls OpenGL
  282. directly, instead of using a modern
  283. [extension loader library](@ref context_glext_auto), you should add the `gl`
  284. pkg-config package.
  285. @code{.sh}
  286. cc $(pkg-config --cflags glfw3 gl) -o myprog myprog.c $(pkg-config --libs glfw3 gl)
  287. @endcode
  288. @subsection build_link_xcode With Xcode on macOS
  289. If you are using the dynamic library version of GLFW, add it to the project
  290. dependencies.
  291. If you are using the static library version of GLFW, add it and the Cocoa,
  292. OpenGL and IOKit frameworks to the project as dependencies. They can all be
  293. found in `/System/Library/Frameworks`.
  294. @subsection build_link_osx With command-line or makefile on macOS
  295. It is recommended that you use [pkg-config](@ref build_link_pkgconfig) when
  296. using installed GLFW binaries from the command line on macOS. That way you will
  297. get any new dependencies added automatically. If you still wish to build
  298. manually, you need to add the required frameworks and libraries to your
  299. command-line yourself using the `-l` and `-framework` switches.
  300. If you are using the dynamic GLFW library, which is named `libglfw.3.dylib`, do:
  301. @code{.sh}
  302. cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit
  303. @endcode
  304. If you are using the static library, named `libglfw3.a`, substitute `-lglfw3`
  305. for `-lglfw`.
  306. Note that you do not add the `.framework` extension to a framework when linking
  307. against it from the command-line.
  308. @note Your machine may have `libGL.*.dylib` style OpenGL library, but that is
  309. for the X Window System and will not work with the macOS native version of GLFW.
  310. */