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