• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Building applications {#build_guide}
2
3[TOC]
4
5This is about compiling and linking applications that use GLFW.  For information on
6how to write such applications, start with the
7[introductory tutorial](@ref quick_guide).  For information on how to compile
8the GLFW library itself, see @ref compile_guide.
9
10This is not a tutorial on compilation or linking.  It assumes basic
11understanding of how to compile and link a C program as well as how to use the
12specific compiler of your chosen development environment.  The compilation
13and linking process should be explained in your C programming material and in
14the documentation for your development environment.
15
16
17## Including the GLFW header file {#build_include}
18
19You should include the GLFW header in the source files where you use OpenGL or
20GLFW.
21
22```c
23#include <GLFW/glfw3.h>
24```
25
26This header defines all the constants and declares all the types and function
27prototypes of the GLFW API.  By default, it also includes the OpenGL header from
28your development environment.  See [option macros](@ref build_macros) below for
29how to select OpenGL ES headers and more.
30
31The GLFW header also defines any platform-specific macros needed by your OpenGL
32header, so that it can be included without needing any window system headers.
33
34It does this only when needed, so if window system headers are included, the
35GLFW header does not try to redefine those symbols.  The reverse is not true,
36i.e. `windows.h` cannot cope if any Win32 symbols have already been defined.
37
38In other words:
39
40 - Use the GLFW header to include OpenGL or OpenGL ES headers portably
41 - Do not include window system headers unless you will use those APIs directly
42 - If you do need such headers, include them before the GLFW header
43
44If you are using an OpenGL extension loading library such as [glad][], the
45extension loader header should be included before the GLFW one.  GLFW attempts
46to detect any OpenGL or OpenGL ES header or extension loader header included
47before it and will then disable the inclusion of the default OpenGL header.
48Most extension loaders also define macros that disable similar headers below it.
49
50[glad]: https://github.com/Dav1dde/glad
51
52```c
53#include <glad/gl.h>
54#include <GLFW/glfw3.h>
55```
56
57Both of these mechanisms depend on the extension loader header defining a known
58macro.  If yours doesn't or you don't know which one your users will pick, the
59@ref GLFW_INCLUDE_NONE macro will explicitly prevent the GLFW header from
60including the OpenGL header.  This will also allow you to include the two
61headers in any order.
62
63```c
64#define GLFW_INCLUDE_NONE
65#include <GLFW/glfw3.h>
66#include <glad/gl.h>
67```
68
69
70### GLFW header option macros {#build_macros}
71
72These macros may be defined before the inclusion of the GLFW header and affect
73its behavior.
74
75@anchor GLFW_DLL
76__GLFW_DLL__ is required on Windows when using the GLFW DLL, to tell the
77compiler that the GLFW functions are defined in a DLL.
78
79The following macros control which OpenGL or OpenGL ES API header is included.
80Only one of these may be defined at a time.
81
82@note GLFW does not provide any of the API headers mentioned below.  They are
83provided by your development environment or your OpenGL, OpenGL ES or Vulkan
84SDK, and most of them can be downloaded from the [Khronos Registry][registry].
85
86[registry]: https://www.khronos.org/registry/
87
88@anchor GLFW_INCLUDE_GLCOREARB
89__GLFW_INCLUDE_GLCOREARB__ makes the GLFW header include the modern
90`GL/glcorearb.h` header (`OpenGL/gl3.h` on macOS) instead of the regular OpenGL
91header.
92
93@anchor GLFW_INCLUDE_ES1
94__GLFW_INCLUDE_ES1__ makes the GLFW header include the OpenGL ES 1.x `GLES/gl.h`
95header instead of the regular OpenGL header.
96
97@anchor GLFW_INCLUDE_ES2
98__GLFW_INCLUDE_ES2__ makes the GLFW header include the OpenGL ES 2.0
99`GLES2/gl2.h` header instead of the regular OpenGL header.
100
101@anchor GLFW_INCLUDE_ES3
102__GLFW_INCLUDE_ES3__ makes the GLFW header include the OpenGL ES 3.0
103`GLES3/gl3.h` header instead of the regular OpenGL header.
104
105@anchor GLFW_INCLUDE_ES31
106__GLFW_INCLUDE_ES31__ makes the GLFW header include the OpenGL ES 3.1
107`GLES3/gl31.h` header instead of the regular OpenGL header.
108
109@anchor GLFW_INCLUDE_ES32
110__GLFW_INCLUDE_ES32__ makes the GLFW header include the OpenGL ES 3.2
111`GLES3/gl32.h` header instead of the regular OpenGL header.
112
113@anchor GLFW_INCLUDE_NONE
114__GLFW_INCLUDE_NONE__ makes the GLFW header not include any OpenGL or OpenGL ES
115API header.  This is useful in combination with an extension loading library.
116
117If none of the above inclusion macros are defined, the standard OpenGL `GL/gl.h`
118header (`OpenGL/gl.h` on macOS) is included, unless GLFW detects the inclusion
119guards of any OpenGL, OpenGL ES or extension loader header it knows about.
120
121The following macros control the inclusion of additional API headers.  Any
122number of these may be defined simultaneously, and/or together with one of the
123above macros.
124
125@anchor GLFW_INCLUDE_VULKAN
126__GLFW_INCLUDE_VULKAN__ makes the GLFW header include the Vulkan
127`vulkan/vulkan.h` header in addition to any selected OpenGL or OpenGL ES header.
128
129@anchor GLFW_INCLUDE_GLEXT
130__GLFW_INCLUDE_GLEXT__ makes the GLFW header include the appropriate extension
131header for the OpenGL or OpenGL ES header selected above after and in addition
132to that header.
133
134@anchor GLFW_INCLUDE_GLU
135__GLFW_INCLUDE_GLU__ makes the header include the GLU header in addition to the
136header selected above.  This should only be used with the standard OpenGL header
137and only for compatibility with legacy code.  GLU has been deprecated and should
138not be used in new code.
139
140@note None of these macros may be defined during the compilation of GLFW itself.
141If your build includes GLFW and you define any these in your build files, make
142sure they are not applied to the GLFW sources.
143
144
145## Link with the right libraries {#build_link}
146
147GLFW is essentially a wrapper of various platform-specific APIs and therefore
148needs to link against many different system libraries.  If you are using GLFW as
149a shared library / dynamic library / DLL then it takes care of these links.
150However, if you are using GLFW as a static library then your executable will
151need to link against these libraries.
152
153On Windows and macOS, the list of system libraries is static and can be
154hard-coded into your build environment.  See the section for your development
155environment below.  On Linux and other Unix-like operating systems, the list
156varies but can be retrieved in various ways as described below.
157
158A good general introduction to linking is [Beginner's Guide to
159Linkers][linker_guide] by David Drysdale.
160
161[linker_guide]: https://www.lurklurk.org/linkers/linkers.html
162
163
164### With Visual C++ and GLFW binaries {#build_link_win32}
165
166If you are using a downloaded [binary
167archive](https://www.glfw.org/download.html), first make sure you have the
168archive matching the architecture you are building for (32-bit or 64-bit), or
169you will get link errors.  Also make sure you are using the binaries for your
170version of Visual C++ or you may get other link errors.
171
172There are two version of the static GLFW library in the binary archive, because
173it needs to use the same base run-time library variant as the rest of your
174executable.
175
176One is named `glfw3.lib` and is for projects with the _Runtime Library_ project
177option set to _Multi-threaded DLL_ or _Multi-threaded Debug DLL_.  The other is
178named `glfw3_mt.lib` and is for projects with _Runtime Library_ set to
179_Multi-threaded_ or _Multi-threaded Debug_.  To use the static GLFW library you
180will need to add `path/to/glfw3.lib` or `path/to/glfw3_mt.lib` to the
181_Additional Dependencies_ project option.
182
183If you compiled a GLFW static library yourself then there will only be one,
184named `glfw3.lib`, and you have to make sure the run-time library variant
185matches.
186
187The DLL version of the GLFW library is named `glfw3.dll`, but you will be
188linking against the `glfw3dll.lib` link library.  To use the DLL you will need
189to add `path/to/glfw3dll.lib` to the _Additional Dependencies_ project option.
190All of its dependencies are already listed there by default, but when building
191with the DLL version of GLFW, you also need to define the @ref GLFW_DLL.  This
192can be done either in the _Preprocessor Definitions_ project option or by
193defining it in your source code before including the GLFW header.
194
195```c
196#define GLFW_DLL
197#include <GLFW/glfw3.h>
198```
199
200All link-time dependencies for GLFW are already listed in the _Additional
201Dependencies_ option by default.
202
203
204### With MinGW-w64 and GLFW binaries {#build_link_mingw}
205
206This is intended for building a program from the command-line or by writing
207a makefile, on Windows with [MinGW-w64][] and GLFW binaries.  These can be from
208a downloaded and extracted [binary archive](https://www.glfw.org/download.html)
209or by compiling GLFW yourself.  The paths below assume a binary archive is used.
210
211If you are using a downloaded binary archive, first make sure you have the
212archive matching the architecture you are building for (32-bit or 64-bit) or you
213will get link errors.
214
215Note that the order of source files and libraries matter for GCC.  Dependencies
216must be listed after the files that depend on them.  Any source files that
217depend on GLFW must be listed before the GLFW library.  GLFW in turn depends on
218`gdi32` and must be listed before it.
219
220[MinGW-w64]: https://www.mingw-w64.org/
221
222If you are using the static version of the GLFW library, which is named
223`libglfw3.a`, do:
224
225```sh
226gcc -o myprog myprog.c -I path/to/glfw/include path/to/glfw/lib-mingw-w64/libglfw3.a -lgdi32
227```
228
229If you are using the DLL version of the GLFW library, which is named
230`glfw3.dll`, you will need to use the `libglfw3dll.a` link library.
231
232```sh
233gcc -o myprog myprog.c -I path/to/glfw/include path/to/glfw/lib-mingw-w64/libglfw3dll.a -lgdi32
234```
235
236The resulting executable will need to find `glfw3.dll` to run, typically by
237keeping both files in the same directory.
238
239When you are building with the DLL version of GLFW, you will also need to define
240the @ref GLFW_DLL macro.  This can be done in your source files, as long as it
241done before including the GLFW header:
242
243```c
244#define GLFW_DLL
245#include <GLFW/glfw3.h>
246```
247
248It can also be done on the command-line:
249
250```sh
251gcc -o myprog myprog.c -D GLFW_DLL -I path/to/glfw/include path/to/glfw/lib-mingw-w64/libglfw3dll.a -lgdi32
252```
253
254
255### With CMake and GLFW source {#build_link_cmake_source}
256
257This section is about using CMake to compile and link GLFW along with your
258application.  If you want to use an installed binary instead, see @ref
259build_link_cmake_package.
260
261With a few changes to your `CMakeLists.txt` you can have the GLFW source tree
262built along with your application.
263
264Add the root directory of the GLFW source tree to your project.  This will add
265the `glfw` target to your project.
266
267```cmake
268add_subdirectory(path/to/glfw)
269```
270
271Once GLFW has been added, link your application against the `glfw` target.
272This adds the GLFW library and its link-time dependencies as it is currently
273configured, the include directory for the GLFW header and, when applicable, the
274@ref GLFW_DLL macro.
275
276```cmake
277target_link_libraries(myapp glfw)
278```
279
280Note that the `glfw` target does not depend on OpenGL, as GLFW loads any OpenGL,
281OpenGL ES or Vulkan libraries it needs at runtime.  If your application calls
282OpenGL directly, instead of using a modern
283[extension loader library](@ref context_glext_auto), use the OpenGL CMake
284package.
285
286```cmake
287find_package(OpenGL REQUIRED)
288```
289
290If OpenGL is found, the `OpenGL::GL` target is added to your project, containing
291library and include directory paths.  Link against this like any other library.
292
293```cmake
294target_link_libraries(myapp OpenGL::GL)
295```
296
297For a minimal example of a program and GLFW sources built with CMake, see the
298[GLFW CMake Starter][cmake_starter] on GitHub.
299
300[cmake_starter]: https://github.com/juliettef/GLFW-CMake-starter
301
302
303### With CMake and installed GLFW binaries {#build_link_cmake_package}
304
305This section is about using CMake to link GLFW after it has been built and
306installed.  If you want to build it along with your application instead, see
307@ref build_link_cmake_source.
308
309With a few changes to your `CMakeLists.txt` you can locate the package and
310target files generated when GLFW is installed.
311
312```cmake
313find_package(glfw3 3.5 REQUIRED)
314```
315
316Once GLFW has been added to the project, link against it with the `glfw` target.
317This adds the GLFW library and its link-time dependencies, the include directory
318for the GLFW header and, when applicable, the @ref GLFW_DLL macro.
319
320```cmake
321target_link_libraries(myapp glfw)
322```
323
324Note that the `glfw` target does not depend on OpenGL, as GLFW loads any OpenGL,
325OpenGL ES or Vulkan libraries it needs at runtime.  If your application calls
326OpenGL directly, instead of using a modern
327[extension loader library](@ref context_glext_auto), use the OpenGL CMake
328package.
329
330```cmake
331find_package(OpenGL REQUIRED)
332```
333
334If OpenGL is found, the `OpenGL::GL` target is added to your project, containing
335library and include directory paths.  Link against this like any other library.
336
337```cmake
338target_link_libraries(myapp OpenGL::GL)
339```
340
341
342### With pkg-config and GLFW binaries on Unix {#build_link_pkgconfig}
343
344This is intended for building a program from the command-line or by writing
345a makefile, on macOS or any Unix-like system like Linux, FreeBSD and Cygwin.
346
347GLFW supports [pkg-config][], and the `glfw3.pc` pkg-config file is generated
348when the GLFW library is built and is installed along with it.  A pkg-config
349file describes all necessary compile-time and link-time flags and dependencies
350needed to use a library.  When they are updated or if they differ between
351systems, you will get the correct ones automatically.
352
353[pkg-config]: https://www.freedesktop.org/wiki/Software/pkg-config/
354
355A typical compile and link command-line when using the static version of the
356GLFW library may look like this:
357
358```sh
359cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --static --libs glfw3)
360```
361
362If you are using the shared version of the GLFW library, omit the `--static`
363flag.
364
365```sh
366cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --libs glfw3)
367```
368
369You can also use the `glfw3.pc` file without installing it first, by using the
370`PKG_CONFIG_PATH` environment variable.
371
372```sh
373env PKG_CONFIG_PATH=path/to/glfw/src cc $(pkg-config --cflags glfw3) -o myprog myprog.c $(pkg-config --libs glfw3)
374```
375
376The dependencies do not include OpenGL, as GLFW loads any OpenGL, OpenGL ES or
377Vulkan libraries it needs at runtime.  If your application calls OpenGL
378directly, instead of using a modern
379[extension loader library](@ref context_glext_auto), you should add the `gl`
380pkg-config package.
381
382```sh
383cc $(pkg-config --cflags glfw3 gl) -o myprog myprog.c $(pkg-config --libs glfw3 gl)
384```
385
386
387### With Xcode on macOS {#build_link_xcode}
388
389If you are using the dynamic library version of GLFW, add it to the project
390dependencies.
391
392If you are using the static library version of GLFW, add it and the Cocoa,
393OpenGL, IOKit and QuartzCore frameworks to the project as dependencies.  They
394can all be found in `/System/Library/Frameworks`.
395
396
397### With command-line or makefile on macOS {#build_link_osx}
398
399It is recommended that you use [pkg-config](@ref build_link_pkgconfig) when
400using installed GLFW binaries from the command line on macOS.  That way you will
401get any new dependencies added automatically.  If you still wish to build
402manually, you need to add the required frameworks and libraries to your
403command-line yourself using the `-l` and `-framework` switches.
404
405If you are using the dynamic GLFW library, which is named `libglfw.3.dylib`, do:
406
407```sh
408cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework QuartzCore
409```
410
411If you are using the static library, named `libglfw3.a`, substitute `-lglfw3`
412for `-lglfw`.
413
414Note that you do not add the `.framework` extension to a framework when linking
415against it from the command-line.
416
417@note Your machine may have `libGL.*.dylib` style OpenGL library, but that is
418for the X Window System and will not work with the macOS native version of GLFW.
419
420