2013-06-12 14:06:45 +00:00
|
|
|
/*!
|
|
|
|
|
2013-10-27 11:50:33 +00:00
|
|
|
@page build Building programs that use GLFW
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
@tableofcontents
|
|
|
|
|
|
|
|
This is about compiling and linking programs that use GLFW. For information on
|
2013-10-21 20:23:15 +00:00
|
|
|
how to write such programs, start with the [introductory tutorial](@ref quick).
|
2013-10-27 11:50:33 +00:00
|
|
|
For information on how to compile the GLFW library itself, see the @ref compile
|
|
|
|
guide.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
This is not a tutorial on compilation. It assumes basic understanding of how to
|
|
|
|
compile a C program as well as how to use the specific compiler of your chosen
|
|
|
|
development environment. The compilation process should be explained in your
|
|
|
|
C programming material and the use of and options for your compiler should be
|
|
|
|
described in detail in the documentation for your development environment.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
@section build_include Including the GLFW header file
|
|
|
|
|
|
|
|
In the files of your program where you use OpenGL or GLFW, you should include
|
2014-04-23 11:30:11 +00:00
|
|
|
the GLFW header file, i.e.:
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
@code
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
@endcode
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
The GLFW header declares the GLFW API and by default also includes the OpenGL
|
|
|
|
header of your development environment, which in turn defines all the constants,
|
|
|
|
types and function prototypes of the OpenGL API.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
The GLFW header also defines everything necessary for your OpenGL header to
|
|
|
|
function. For example, under Windows you are normally required to include
|
|
|
|
`windows.h` before the OpenGL header. This would make your source file tied
|
|
|
|
to Windows and pollute your code's namespace with the whole Win32 API.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
Instead, the GLFW header takes care of this for you, not by including
|
2014-04-23 11:30:11 +00:00
|
|
|
`windows.h`, but by duplicating only the very few necessary parts of it. It
|
|
|
|
does this only when needed, so if `windows.h` *is* included, the GLFW header
|
2013-06-12 14:06:45 +00:00
|
|
|
does not try to redefine those symbols.
|
|
|
|
|
|
|
|
In other words:
|
|
|
|
|
|
|
|
- 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
|
|
|
|
plan on using those APIs directly
|
|
|
|
- If you *do* need to include such headers, do it *before* including
|
|
|
|
the GLFW one and it will detect this
|
|
|
|
|
|
|
|
If you are using an OpenGL extension loading library such as
|
2014-04-23 11:30:11 +00:00
|
|
|
[glad](https://github.com/Dav1dde/glad), the extension loader header should
|
|
|
|
either be included *before* the GLFW one, or the `GLFW_INCLUDE_NONE` macro
|
|
|
|
(described below) should be defined.
|
2013-06-12 14:06:45 +00: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 11:30:11 +00:00
|
|
|
its behavior.
|
|
|
|
|
|
|
|
`GLFW_DLL` is required on Windows when using the GLFW DLL, to tell the compiler
|
|
|
|
that the GLFW functions are defined in a DLL.
|
|
|
|
|
|
|
|
The following macros control which client API header is included.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
`GLFW_INCLUDE_GLCOREARB` makes the header include the modern `GL/glcorearb.h`
|
2013-07-04 11:05:19 +00:00
|
|
|
header (`OpenGL/gl3.h` on OS X) instead of the regular OpenGL header.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
`GLFW_INCLUDE_ES1` makes the header include the OpenGL ES 1.x `GLES/gl.h` header
|
|
|
|
instead of the regular OpenGL header.
|
|
|
|
|
|
|
|
`GLFW_INCLUDE_ES2` makes the header include the OpenGL ES 2.0 `GLES2/gl2.h`
|
|
|
|
header instead of the regular OpenGL header.
|
|
|
|
|
|
|
|
`GLFW_INCLUDE_ES3` makes the header include the OpenGL ES 3.0 `GLES3/gl3.h`
|
|
|
|
header instead of the regular OpenGL header.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
`GLFW_INCLUDE_ES31` makes the header include the OpenGL ES 3.1 `GLES3/gl31.h`
|
|
|
|
header instead of the regular OpenGL header.
|
|
|
|
|
|
|
|
`GLFW_INCLUDE_NONE` makes the header not include any client API header. This is
|
|
|
|
useful in combination with an extension loading library.
|
|
|
|
|
|
|
|
If none of the above inclusion macros are defined, the standard OpenGL header is
|
|
|
|
included.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2013-07-04 11:05:19 +00:00
|
|
|
`GLFW_INCLUDE_GLU` makes the header include the GLU header *in addition to* the
|
2014-04-23 11:30:11 +00:00
|
|
|
header selected above. This should only be used with legacy code. GLU has been
|
|
|
|
deprecated and should not be used in new code.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@note GLFW does not provide any of the API headers mentioned above. They must
|
|
|
|
be provided by your development environment or your OpenGL or OpenGL ES SDK.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@section build_link Link with the right libraries
|
|
|
|
|
2014-04-23 11:30:11 +00: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 OS X, the list of system libraries is static and can be
|
|
|
|
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.
|
|
|
|
|
|
|
|
This is not a tutorial on linking. It assumes basic understanding of how to
|
|
|
|
link a C program as well as how to use the specific linker of your chosen
|
|
|
|
development environment. The linking process should be explained in your
|
|
|
|
C programming material and the use of and options for your linker should be
|
|
|
|
described in detail in the documentation for your development environment.
|
|
|
|
|
|
|
|
A good general introduction to linking is
|
2014-04-13 16:07:36 +00:00
|
|
|
[Beginner's Guide to Linkers](http://www.lurklurk.org/linkers/linkers.html) by
|
|
|
|
David Drysdale.
|
|
|
|
|
|
|
|
|
2013-06-23 13:17:43 +00:00
|
|
|
@subsection build_link_win32 With MinGW or Visual C++ on Windows
|
2013-06-12 14:06:45 +00: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 linking a program under Windows that uses the static version of GLFW, you
|
2013-12-22 23:52:28 +00:00
|
|
|
must link with `opengl32`. On some versions of MinGW, you must also explicitly
|
|
|
|
link with `gdi32`, while other versions of MinGW 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 14:06:45 +00:00
|
|
|
|
|
|
|
The link library for the GLFW DLL is named `glfw3dll`. When compiling a program
|
|
|
|
that uses the DLL version of GLFW, you need to define the `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.
|
|
|
|
|
|
|
|
A program using the GLFW DLL does not need to link against any of its
|
|
|
|
dependencies, but you still have to link against `opengl32` if your program uses
|
|
|
|
OpenGL and `glu32` if it uses GLU.
|
|
|
|
|
|
|
|
|
2013-06-23 13:17:43 +00:00
|
|
|
@subsection build_link_cmake_source With CMake and GLFW source
|
2013-06-21 11:04:47 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
With just a few changes to your `CMakeLists.txt` you can have the GLFW source
|
|
|
|
tree built along with your application.
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
Firstly, add the root directory of the GLFW source tree to your project. This
|
|
|
|
will add the `glfw` target and the necessary cache variables to your project.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
add_subdirectory(path/to/glfw)
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
To be able to include the GLFW header from your code, you need to tell the
|
|
|
|
compiler where to find it.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
include_directories(path/to/glfw/include)
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
Once GLFW has been added to the project, the `GLFW_LIBRARIES` cache variable
|
|
|
|
contains all link-time dependencies of GLFW as it is currently configured. To
|
|
|
|
link against GLFW, link against them and the `glfw` target.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
target_link_libraries(myapp glfw ${GLFW_LIBRARIES})
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
Note that `GLFW_LIBRARIES` does not include GLU, as GLFW does not use it. If
|
|
|
|
your application needs GLU, you can add it to the list of dependencies with the
|
|
|
|
`OPENGL_glu_LIBRARY` cache variable, which is implicitly created when the GLFW
|
|
|
|
CMake files look for OpenGL.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
target_link_libraries(myapp glfw ${OPENGL_glu_LIBRARY} ${GLFW_LIBRARIES})
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
|
2013-06-23 13:17:43 +00:00
|
|
|
@subsection build_link_cmake_pkgconfig With CMake on Unix and installed GLFW binaries
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
CMake can import settings from pkg-config, which GLFW supports. When you
|
|
|
|
installed GLFW, the pkg-config file `glfw3.pc` was installed along with it.
|
|
|
|
|
|
|
|
First you need to find the PkgConfig package. If this fails, you may need to
|
|
|
|
install the pkg-config package for your distribution.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
find_package(PkgConfig REQUIRED)
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
This creates the CMake commands to find pkg-config packages. Then you need to
|
|
|
|
find the GLFW package.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
pkg_search_module(GLFW REQUIRED glfw3)
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
This creates the CMake variables you need to use GLFW. To be able to include
|
|
|
|
the GLFW header, you need to tell your compiler where it is.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
include_directories(${GLFW_INCLUDE_DIRS})
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
You also need to link against the correct libraries. If you are using the
|
|
|
|
shared library version of GLFW, use the `GLFW_LIBRARIES` variable.
|
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
target_link_libraries(simple ${GLFW_LIBRARIES})
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
If you are using the static library version of GLFW, use the
|
2013-06-23 13:17:43 +00:00
|
|
|
`GLFW_STATIC_LIBRARIES` variable instead.
|
2013-06-21 11:04:47 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.cmake}
|
|
|
|
target_link_libraries(simple ${GLFW_STATIC_LIBRARIES})
|
|
|
|
@endcode
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@subsection build_link_pkgconfig With pkg-config on OS X or other Unix
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
GLFW supports [pkg-config](http://www.freedesktop.org/wiki/Software/pkg-config/),
|
2014-04-23 11:30:11 +00:00
|
|
|
and the `glfw3.pc` pkf-config file is generated when the GLFW library is built
|
|
|
|
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 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
A typical compile and link command-line when using the static version of the
|
|
|
|
GLFW library may look like this:
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.sh}
|
|
|
|
cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --static --libs glfw3`
|
|
|
|
@endcode
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
If you are using the shared version of the GLFW library, simply omit the
|
|
|
|
`--static` flag.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.sh}
|
|
|
|
cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`
|
|
|
|
@endcode
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2013-06-21 11:04:47 +00:00
|
|
|
You can also use the `glfw3.pc` file without installing it first, by using the
|
|
|
|
`PKG_CONFIG_PATH` environment variable.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00: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 14:06:45 +00:00
|
|
|
|
2013-06-21 11:04:47 +00:00
|
|
|
The dependencies do not include GLU, as GLFW does not need it. On OS X, GLU is
|
|
|
|
built into the OpenGL framework, so if you need GLU you don't need to do
|
2014-04-23 11:30:11 +00:00
|
|
|
anything extra. If you need GLU and are using Linux or BSD, you should add the
|
|
|
|
`glu` pkg-config module.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
@code{.sh}
|
|
|
|
cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --libs glfw3 glu`
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
If you are using the static version of the GLFW library, make sure you don't link statically against GLU.
|
|
|
|
|
|
|
|
@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 14:06:45 +00:00
|
|
|
|
|
|
|
|
2013-06-21 11:04:47 +00:00
|
|
|
@subsection build_link_xcode With Xcode on OS X
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2013-06-21 11:04:47 +00:00
|
|
|
If you are using the dynamic library version of GLFW, simply add it to the
|
|
|
|
project dependencies.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2013-06-21 11:04:47 +00:00
|
|
|
If you are using the static library version of GLFW, add it and the Cocoa,
|
2014-04-23 11:30:11 +00:00
|
|
|
OpenGL, IOKit and CoreVideo frameworks to the project as dependencies. They can
|
|
|
|
all be found in `/System/Library/Frameworks`.
|
2013-06-21 11:04:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@subsection build_link_osx With command-line on OS X
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
It is recommended that you use [pkg-config](@ref build_link_pkgconfig) when
|
|
|
|
building from the command line on OS X. That way you will get any new
|
|
|
|
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 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
If you are using the static library, named `libglfw3.a`, substitute `-lglfw3`
|
|
|
|
for `-lglfw`.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2014-04-23 11:30:11 +00:00
|
|
|
Note that you do not add the `.framework` extension to a framework when linking
|
|
|
|
against it from the command-line.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
2013-06-21 11:04:47 +00: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
|
2013-07-10 13:26:16 +00:00
|
|
|
will *not* work with the OS X native version of GLFW.
|
2013-06-12 14:06:45 +00:00
|
|
|
|
|
|
|
*/
|