1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-30 05:58:52 -05:00
glfw/docs/build.dox

387 lines
14 KiB
Plaintext
Raw Normal View History

2013-06-12 10:06:45 -04:00
/*!
@page build_guide Building applications
2013-06-12 10:06:45 -04:00
@tableofcontents
2014-09-18 09:03:29 -04:00
This is about compiling and linking applications that use GLFW. For information on
how to write such applications, start with the
[introductory tutorial](@ref quick_guide). For information on how to compile
the GLFW library itself, see @ref compile_guide.
2013-06-12 10:06:45 -04:00
2014-09-18 09:03:29 -04:00
This is not a tutorial on compilation or linking. It assumes basic
understanding of how to compile and link a C program as well as how to use the
specific compiler of your chosen development environment. The compilation
and linking process should be explained in your C programming material and in
the documentation for your development environment.
2013-06-12 10:06:45 -04:00
2017-06-07 06:25:57 -04:00
2013-06-12 10:06:45 -04:00
@section build_include Including the GLFW header file
2017-06-07 06:25:57 -04:00
You should include the GLFW header in the source files where you use OpenGL or
GLFW.
2013-06-12 10:06:45 -04:00
@code
#include <GLFW/glfw3.h>
@endcode
2017-06-07 06:25:57 -04:00
This header declares the GLFW API and by default also includes the OpenGL header
from your development environment. See below for how to control this.
The GLFW header also defines any platform-specific macros needed by your OpenGL
header, so it can be included without needing any window system headers.
2013-06-12 10:06:45 -04:00
2017-06-07 06:25:57 -04:00
For example, under Windows you are normally required to include `windows.h`
before the OpenGL header, which would bring in the whole Win32 API. The GLFW
header duplicates the small number of macros needed.
2013-06-12 10:06:45 -04:00
2017-06-07 06:25:57 -04:00
It does this only when needed, so if `windows.h` _is_ included, the GLFW header
2014-09-18 09:03:29 -04:00
does not try to redefine those symbols. The reverse is not true, i.e.
`windows.h` cannot cope if any of its symbols have already been defined.
2013-06-12 10:06:45 -04:00
In other words:
2014-10-02 11:35:10 -04:00
- Do _not_ include the OpenGL headers yourself, as GLFW does this for you
- Do _not_ include `windows.h` or other platform-specific headers unless you
2013-06-12 10:06:45 -04:00
plan on using those APIs directly
2014-10-02 11:35:10 -04:00
- If you _do_ need to include such headers, do it _before_ including
2014-09-18 09:03:29 -04:00
the GLFW header and it will handle this
2013-06-12 10:06:45 -04:00
If you are using an OpenGL extension loading library such as
2014-04-23 07:30:11 -04:00
[glad](https://github.com/Dav1dde/glad), the extension loader header should
2017-06-07 06:25:57 -04:00
be included _before_ the GLFW one.
@code
#include <glad/gl.h>
2017-06-07 06:25:57 -04:00
#include <GLFW/glfw3.h>
@endcode
Alternatively the @ref GLFW_INCLUDE_NONE macro (described below) can be used to
prevent the GLFW header from including the OpenGL header.
@code
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/gl.h>
2017-06-07 06:25:57 -04:00
@endcode
2013-06-12 10:06:45 -04:00
@subsection build_macros GLFW header option macros
These macros may be defined before the inclusion of the GLFW header and affect
2014-04-23 07:30:11 -04:00
its behavior.
2016-12-05 19:14:23 -05:00
@anchor GLFW_DLL
__GLFW_DLL__ is required on Windows when using the GLFW DLL, to tell the
compiler that the GLFW functions are defined in a DLL.
2014-04-23 07:30:11 -04:00
2014-09-18 09:03:29 -04:00
The following macros control which OpenGL or OpenGL ES API header is included.
Only one of these may be defined at a time.
2013-06-12 10:06:45 -04:00
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_GLCOREARB
__GLFW_INCLUDE_GLCOREARB__ makes the GLFW header include the modern
`GL/glcorearb.h` header (`OpenGL/gl3.h` on macOS) instead of the regular OpenGL
2014-09-18 09:03:29 -04:00
header.
2013-06-12 10:06:45 -04:00
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_ES1
__GLFW_INCLUDE_ES1__ makes the GLFW header include the OpenGL ES 1.x `GLES/gl.h`
2014-09-18 09:03:29 -04:00
header instead of the regular OpenGL header.
2013-06-12 10:06:45 -04:00
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_ES2
__GLFW_INCLUDE_ES2__ makes the GLFW header include the OpenGL ES 2.0
`GLES2/gl2.h` header instead of the regular OpenGL header.
2013-06-12 10:06:45 -04:00
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_ES3
__GLFW_INCLUDE_ES3__ makes the GLFW header include the OpenGL ES 3.0
`GLES3/gl3.h` header instead of the regular OpenGL header.
2013-06-12 10:06:45 -04:00
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_ES31
__GLFW_INCLUDE_ES31__ makes the GLFW header include the OpenGL ES 3.1
`GLES3/gl31.h` header instead of the regular OpenGL header.
2014-04-23 07:30:11 -04:00
2017-01-01 23:58:41 -05:00
@anchor GLFW_INCLUDE_ES32
__GLFW_INCLUDE_ES31__ makes the GLFW header include the OpenGL ES 3.2
`GLES3/gl32.h` header instead of the regular OpenGL header.
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_NONE
__GLFW_INCLUDE_NONE__ makes the GLFW header not include any OpenGL or OpenGL ES
API header. This is useful in combination with an extension loading library.
2014-04-23 07:30:11 -04:00
2014-09-18 09:03:29 -04:00
If none of the above inclusion macros are defined, the standard OpenGL `GL/gl.h`
header (`OpenGL/gl.h` on macOS) is included.
2013-06-12 10:06:45 -04:00
The following macros control the inclusion of additional API headers. Any
number of these may be defined simultaneously, and/or together with one of the
above macros.
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_VULKAN
__GLFW_INCLUDE_VULKAN__ makes the GLFW header include the Vulkan
`vulkan/vulkan.h` header in addition to any selected OpenGL or OpenGL ES header.
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_GLEXT
__GLFW_INCLUDE_GLEXT__ makes the GLFW header include the appropriate extension
header for the OpenGL or OpenGL ES header selected above after and in addition
to that header.
2016-12-05 19:14:23 -05:00
@anchor GLFW_INCLUDE_GLU
__GLFW_INCLUDE_GLU__ makes the header include the GLU header in addition to the
header selected above. This should only be used with the standard OpenGL header
2015-10-12 13:04:09 -04:00
and only for compatibility with legacy code. GLU has been deprecated and should
not be used in new code.
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
@note GLFW does not provide any of the API headers mentioned above. They must
2016-08-17 10:48:22 -04:00
be provided by your development environment or your OpenGL, OpenGL ES or Vulkan
SDK.
2013-06-12 10:06:45 -04:00
@note None of these macros may be defined during the compilation of GLFW itself.
If your build includes GLFW and you define any these in your build files, make
sure they are not applied to the GLFW sources.
2013-06-12 10:06:45 -04:00
@section build_link Link with the right libraries
2014-04-23 07:30:11 -04:00
GLFW is essentially a wrapper of various platform-specific APIs and therefore
needs to link against many different system libraries. If you are using GLFW as
a shared library / dynamic library / DLL then it takes care of these links.
However, if you are using GLFW as a static library then your executable will
need to link against these libraries.
On Windows and macOS, the list of system libraries is static and can be
2014-04-23 07:30:11 -04:00
hard-coded into your build environment. See the section for your development
environment below. On Linux and other Unix-like operating systems, the list
varies but can be retrieved in various ways as described below.
A good general introduction to linking is
2018-11-02 15:55:02 -04:00
[Beginner's Guide to Linkers](https://www.lurklurk.org/linkers/linkers.html) by
2014-04-13 12:07:36 -04:00
David Drysdale.
2013-06-23 09:17:43 -04:00
@subsection build_link_win32 With MinGW or Visual C++ on Windows
2013-06-12 10:06:45 -04:00
The static version of the GLFW library is named `glfw3`. When using this
version, it is also necessary to link with some libraries that GLFW uses.
When using MinGW to link an application with the static version of GLFW, you
must also explicitly link with `gdi32`. Other toolchains including MinGW-w64
include it in the set of default libraries along with other dependencies like
`user32` and `kernel32`.
If you are using GLU, you must also link with `glu32`.
2013-06-12 10:06:45 -04:00
2014-09-18 09:03:29 -04:00
The link library for the GLFW DLL is named `glfw3dll`. When compiling an
2016-12-05 19:14:23 -05:00
application that uses the DLL version of GLFW, you need to define the @ref
GLFW_DLL macro _before_ any inclusion of the GLFW header. This can be done
either with a compiler switch or by defining it in your source code.
2013-06-12 10:06:45 -04:00
2014-09-18 09:03:29 -04:00
An application using the GLFW DLL does not need to link against any of its
dependencies, but you still have to link against `glu32` if it uses GLU.
2013-06-12 10:06:45 -04:00
2013-06-23 09:17:43 -04:00
@subsection build_link_cmake_source With CMake and GLFW source
2013-06-21 07:04:47 -04:00
2016-02-01 20:24:12 -05:00
This section is about using CMake to compile and link GLFW along with your
application. If you want to use an installed binary instead, see @ref
2016-05-29 11:37:32 -04:00
build_link_cmake_package.
2016-02-01 20:24:12 -05:00
2017-10-22 10:27:17 -04:00
With a few changes to your `CMakeLists.txt` you can have the GLFW source tree
built along with your application.
2013-06-21 07:04:47 -04:00
When including GLFW as part of your build, you probably don't want to build the
GLFW tests, examples and documentation. To disable these, set the corresponding
cache variables before adding the GLFW source tree.
@code
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
@endcode
Then add the root directory of the GLFW source tree to your project. This
2013-06-21 07:04:47 -04:00
will add the `glfw` target and the necessary cache variables to your project.
2014-04-23 07:30:11 -04:00
@code{.cmake}
add_subdirectory(path/to/glfw)
@endcode
2013-06-21 07:04:47 -04:00
Once GLFW has been added to the project, link against it with the `glfw` target.
This adds all link-time dependencies of GLFW as it is currently configured,
2016-12-05 19:14:23 -05:00
the include directory for the GLFW header and, when applicable, the @ref
GLFW_DLL macro.
@code{.cmake}
target_link_libraries(myapp glfw)
@endcode
2016-08-17 10:48:22 -04:00
Note that the dependencies do not include OpenGL or GLU, as GLFW loads any
OpenGL, OpenGL ES or Vulkan libraries it needs at runtime and does not use GLU.
If your application calls OpenGL directly, instead of using a modern
[extension loader library](@ref context_glext_auto) you can find it by requiring
the OpenGL package.
2013-06-21 07:04:47 -04:00
2014-04-23 07:30:11 -04:00
@code{.cmake}
find_package(OpenGL REQUIRED)
2014-04-23 07:30:11 -04:00
@endcode
2013-06-21 07:04:47 -04:00
2016-08-17 10:48:22 -04:00
If OpenGL is found, the `OPENGL_FOUND` variable is true and the
`OPENGL_INCLUDE_DIR` and `OPENGL_gl_LIBRARY` cache variables can be used.
2013-06-21 07:04:47 -04:00
2014-04-23 07:30:11 -04:00
@code{.cmake}
target_include_directories(myapp PUBLIC ${OPENGL_INCLUDE_DIR})
2016-08-17 10:48:22 -04:00
target_link_libraries(myapp ${OPENGL_gl_LIBRARY})
@endcode
The OpenGL CMake package also looks for GLU. If GLU is found, the
`OPENGL_GLU_FOUND` variable is true and the `OPENGL_INCLUDE_DIR` and
`OPENGL_glu_LIBRARY` cache variables can be used.
@code{.cmake}
2016-02-01 20:24:12 -05:00
target_link_libraries(myapp ${OPENGL_glu_LIBRARY})
2014-04-23 07:30:11 -04:00
@endcode
2013-06-21 07:04:47 -04:00
2016-08-17 10:48:22 -04:00
@note GLU has been deprecated and should not be used in new code, but some
2017-01-24 15:24:09 -05:00
legacy code requires it. See the [section on GLU](@ref moving_glu) in the
transition guide for suggested replacements.
2016-08-17 10:48:22 -04:00
2013-06-21 07:04:47 -04:00
2016-05-29 11:37:32 -04:00
@subsection build_link_cmake_package With CMake and installed GLFW binaries
2013-06-21 07:04:47 -04:00
2016-02-01 20:24:12 -05:00
This section is about using CMake to link GLFW after it has been built and
installed. If you want to build it along with your application instead, see
@ref build_link_cmake_source.
2013-06-21 07:04:47 -04:00
2017-10-22 10:27:17 -04:00
With a few changes to your `CMakeLists.txt` you can locate the package and
2016-02-01 20:24:12 -05:00
target files generated when GLFW is installed.
2013-06-21 07:04:47 -04:00
2014-04-23 07:30:11 -04:00
@code{.cmake}
2016-08-18 17:42:15 -04:00
find_package(glfw3 3.3 REQUIRED)
2014-04-23 07:30:11 -04:00
@endcode
2013-06-21 07:04:47 -04:00
2017-04-05 20:02:16 -04:00
Once GLFW has been added to the project, link against it with the `glfw` target.
This adds all link-time dependencies of GLFW as it is currently configured,
the include directory for the GLFW header and, when applicable, the @ref
GLFW_DLL macro.
@code{.cmake}
target_link_libraries(myapp glfw)
@endcode
2016-08-17 10:48:22 -04:00
Note that the dependencies do not include OpenGL or GLU, as GLFW loads any
OpenGL, OpenGL ES or Vulkan libraries it needs at runtime and does not use GLU.
If your application calls OpenGL directly, instead of using a modern
[extension loader library](@ref context_glext_auto) you can find it by requiring
the OpenGL package.
2013-06-21 07:04:47 -04:00
2014-04-23 07:30:11 -04:00
@code{.cmake}
2016-08-17 10:48:22 -04:00
find_package(OpenGL REQUIRED)
2014-04-23 07:30:11 -04:00
@endcode
2013-06-21 07:04:47 -04:00
2016-08-17 10:48:22 -04:00
If OpenGL is found, the `OPENGL_FOUND` variable is true and the
`OPENGL_INCLUDE_DIR` and `OPENGL_gl_LIBRARY` cache variables can be used.
2013-06-21 07:04:47 -04:00
2014-04-23 07:30:11 -04:00
@code{.cmake}
target_include_directories(myapp PUBLIC ${OPENGL_INCLUDE_DIR})
2016-08-17 10:48:22 -04:00
target_link_libraries(myapp ${OPENGL_gl_LIBRARY})
2014-04-23 07:30:11 -04:00
@endcode
2013-06-21 07:04:47 -04:00
2016-08-17 10:48:22 -04:00
The OpenGL CMake package also looks for GLU. If GLU is found, the
`OPENGL_GLU_FOUND` variable is true and the `OPENGL_INCLUDE_DIR` and
`OPENGL_glu_LIBRARY` cache variables can be used.
2013-06-21 07:04:47 -04:00
2014-04-23 07:30:11 -04:00
@code{.cmake}
2016-02-01 20:24:12 -05:00
target_link_libraries(myapp ${OPENGL_glu_LIBRARY})
2014-04-23 07:30:11 -04:00
@endcode
2013-06-21 07:04:47 -04:00
2016-08-17 10:48:22 -04:00
@note GLU has been deprecated and should not be used in new code, but some
2017-01-24 15:24:09 -05:00
legacy code requires it. See the [section on GLU](@ref moving_glu) in the
transition guide for suggested replacements.
2016-08-17 10:48:22 -04:00
2013-06-21 07:04:47 -04:00
2016-02-01 20:24:12 -05:00
@subsection build_link_pkgconfig With makefiles and pkg-config on Unix
2013-06-12 10:06:45 -04:00
2018-11-02 15:55:02 -04:00
GLFW supports [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/),
2016-03-31 10:56:43 -04:00
and the `glfw3.pc` pkg-config file is generated when the GLFW library is built
2014-04-23 07:30:11 -04:00
and is installed along with it. A pkg-config file describes all necessary
compile-time and link-time flags and dependencies needed to use a library. When
they are updated or if they differ between systems, you will get the correct
ones automatically.
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
A typical compile and link command-line when using the static version of the
GLFW library may look like this:
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
@code{.sh}
cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --static --libs glfw3`
@endcode
2013-06-12 10:06:45 -04:00
2017-10-22 10:27:17 -04:00
If you are using the shared version of the GLFW library, omit the `--static`
flag.
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
@code{.sh}
cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`
@endcode
2013-06-12 10:06:45 -04:00
2013-06-21 07:04:47 -04:00
You can also use the `glfw3.pc` file without installing it first, by using the
`PKG_CONFIG_PATH` environment variable.
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
@code{.sh}
env PKG_CONFIG_PATH=path/to/glfw/src cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`
@endcode
2013-06-12 10:06:45 -04:00
2016-08-17 10:48:22 -04:00
The dependencies do not include OpenGL or GLU, as GLFW loads any OpenGL, OpenGL
ES or Vulkan libraries it needs at runtime and does not use GLU. On macOS, GLU
2016-08-17 10:48:22 -04:00
is built into the OpenGL framework, so if you need GLU you don't need to do
2014-04-23 07:30:11 -04:00
anything extra. If you need GLU and are using Linux or BSD, you should add the
2016-05-29 11:37:32 -04:00
`glu` pkg-config package.
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
@code{.sh}
cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --libs glfw3 glu`
@endcode
2016-08-17 10:48:22 -04:00
@note GLU has been deprecated and should not be used in new code, but some
2017-01-24 15:24:09 -05:00
legacy code requires it. See the [section on GLU](@ref moving_glu) in the
transition guide for suggested replacements.
2016-08-17 10:48:22 -04:00
2015-10-12 13:04:09 -04:00
If you are using the static version of the GLFW library, make sure you don't
link statically against GLU.
2014-04-23 07:30:11 -04:00
@code{.sh}
cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --static --libs glfw3` `pkg-config --libs glu`
@endcode
2013-06-12 10:06:45 -04:00
@subsection build_link_xcode With Xcode on macOS
2013-06-12 10:06:45 -04:00
2017-10-22 10:27:17 -04:00
If you are using the dynamic library version of GLFW, add it to the project
dependencies.
2013-06-12 10:06:45 -04:00
2013-06-21 07:04:47 -04:00
If you are using the static library version of GLFW, add it and the Cocoa,
OpenGL, IOKit and CoreVideo frameworks to the project as dependencies. They can
all be found in `/System/Library/Frameworks`.
2013-06-21 07:04:47 -04:00
@subsection build_link_osx With command-line on macOS
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
It is recommended that you use [pkg-config](@ref build_link_pkgconfig) when
building from the command line on macOS. That way you will get any new
2014-04-23 07:30:11 -04:00
dependencies added automatically. If you still wish to build manually, you need
to add the required frameworks and libraries to your command-line yourself using
the `-l` and `-framework` switches.
If you are using the dynamic GLFW library, which is named `libglfw.3.dylib`, do:
@code{.sh}
cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
@endcode
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
If you are using the static library, named `libglfw3.a`, substitute `-lglfw3`
for `-lglfw`.
2013-06-12 10:06:45 -04:00
2014-04-23 07:30:11 -04:00
Note that you do not add the `.framework` extension to a framework when linking
against it from the command-line.
2013-06-12 10:06:45 -04:00
2013-06-21 07:04:47 -04:00
The OpenGL framework contains both the OpenGL and GLU APIs, so there is nothing
special to do when using GLU. Also note that even though your machine may have
`libGL`-style OpenGL libraries, they are for use with the X Window System and
will _not_ work with the macOS native version of GLFW.
2013-06-12 10:06:45 -04:00
*/