<trid="row_1_"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><spanclass="icona"><spanclass="icon">C</span></span><aclass="el"href="structGLFWimage.html"target="_self">GLFWimage</a></td><tdclass="desc">Image data </td></tr>
<trid="row_2_"class="even"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><spanclass="icona"><spanclass="icon">C</span></span><aclass="el"href="structGLFWvidmode.html"target="_self">GLFWvidmode</a></td><tdclass="desc">Video mode type </td></tr>
<dd>On some Linux systems, creating contexts via both the native and EGL APIs in a single process will cause the application to segfault. Stick to one API or the other on Linux for now.</dd>
<ul><liclass="level1"><ahref="#build_include">Including the GLFW header file</a><ul><liclass="level2"><ahref="#build_macros">GLFW header option macros</a></li>
</ul>
</li>
<liclass="level1"><ahref="#build_link">Link with the right libraries</a><ul><liclass="level2"><ahref="#build_link_win32">With MinGW or Visual C++ on Windows</a></li>
<liclass="level2"><ahref="#build_link_cmake_source">With CMake and GLFW source</a></li>
<liclass="level2"><ahref="#build_link_cmake_package">With CMake and installed GLFW binaries</a></li>
<liclass="level2"><ahref="#build_link_pkgconfig">With makefiles and pkg-config on Unix</a></li>
<liclass="level2"><ahref="#build_link_xcode">With Xcode on OS X</a></li>
<liclass="level2"><ahref="#build_link_osx">With command-line on OS X</a></li>
</ul>
</li>
</ul>
</div>
<divclass="textblock"><p>This is about compiling and linking applications that use GLFW. For information on how to write such applications, start with the <aclass="el"href="quick_guide.html">introductory tutorial</a>. For information on how to compile the GLFW library itself, see <aclass="el"href="compile_guide.html">Compiling GLFW</a>.</p>
<p>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.</p>
<h1><aclass="anchor"id="build_include"></a>
Including the GLFW header file</h1>
<p>In the source files of your application where you use OpenGL or GLFW, you should include the GLFW header file, i.e.:</p>
<divclass="fragment"><divclass="line"><spanclass="preprocessor">#include <<aclass="code"href="glfw3_8h.html">GLFW/glfw3.h</a>></span></div></div><!-- fragment --><p>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.</p>
<p>The GLFW header also defines everything necessary for your OpenGL header to function. For example, under Windows you are normally required to include <code>windows.h</code> before the OpenGL header, which would pollute your code namespace with the entire Win32 API.</p>
<p>Instead, the GLFW header takes care of this for you, not by including <code>windows.h</code>, but by duplicating only the very few necessary parts of it. It does this only when needed, so if <code>windows.h</code><em>is</em> included, the GLFW header does not try to redefine those symbols. The reverse is not true, i.e. <code>windows.h</code> cannot cope if any of its symbols have already been defined.</p>
<p>In other words:</p>
<ul>
<li>Do <em>not</em> include the OpenGL headers yourself, as GLFW does this for you</li>
<li>Do <em>not</em> include <code>windows.h</code> or other platform-specific headers unless you plan on using those APIs directly</li>
<li>If you <em>do</em> need to include such headers, do it <em>before</em> including the GLFW header and it will handle this</li>
</ul>
<p>If you are using an OpenGL extension loading library such as <ahref="https://github.com/Dav1dde/glad">glad</a>, the extension loader header should either be included <em>before</em> the GLFW one, or the <code>GLFW_INCLUDE_NONE</code> macro (described below) should be defined.</p>
<h2><aclass="anchor"id="build_macros"></a>
GLFW header option macros</h2>
<p>These macros may be defined before the inclusion of the GLFW header and affect its behavior.</p>
<p><code>GLFW_DLL</code> is required on Windows when using the GLFW DLL, to tell the compiler that the GLFW functions are defined in a DLL.</p>
<p>The following macros control which OpenGL or OpenGL ES API header is included. Only one of these may be defined at a time.</p>
<p><code>GLFW_INCLUDE_GLCOREARB</code> makes the GLFW header include the modern <code>GL/glcorearb.h</code> header (<code>OpenGL/gl3.h</code> on OS X) instead of the regular OpenGL header.</p>
<p><code>GLFW_INCLUDE_ES1</code> makes the GLFW header include the OpenGL ES 1.x <code>GLES/gl.h</code> header instead of the regular OpenGL header.</p>
<p><code>GLFW_INCLUDE_ES2</code> makes the GLFW header include the OpenGL ES 2.0 <code>GLES2/gl2.h</code> header instead of the regular OpenGL header.</p>
<p><code>GLFW_INCLUDE_ES3</code> makes the GLFW header include the OpenGL ES 3.0 <code>GLES3/gl3.h</code> header instead of the regular OpenGL header.</p>
<p><code>GLFW_INCLUDE_ES31</code> makes the GLFW header include the OpenGL ES 3.1 <code>GLES3/gl31.h</code> header instead of the regular OpenGL header.</p>
<p><code>GLFW_INCLUDE_VULKAN</code> makes the GLFW header include the Vulkan <code>vulkan/vulkan.h</code> header instead of the regular OpenGL header.</p>
<p><code>GLFW_INCLUDE_NONE</code> makes the GLFW header not include any OpenGL or OpenGL ES API header. This is useful in combination with an extension loading library.</p>
<p>If none of the above inclusion macros are defined, the standard OpenGL <code>GL/gl.h</code> header (<code>OpenGL/gl.h</code> on OS X) is included.</p>
<p>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.</p>
<p><code>GLFW_INCLUDE_GLEXT</code> 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.</p>
<p><code>GLFW_INCLUDE_GLU</code> makes the header include the GLU header in addition to the header selected above. This should only be used with the standard OpenGL header and only for compatibility with legacy code. GLU has been deprecated and should not be used in new code.</p>
<dlclass="section note"><dt>Note</dt><dd>GLFW does not provide any of the API headers mentioned above. They must be provided by your development environment or your OpenGL, OpenGL ES or Vulkan SDK.</dd>
<dd>
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.</dd></dl>
<h1><aclass="anchor"id="build_link"></a>
Link with the right libraries</h1>
<p>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.</p>
<p>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.</p>
<p>A good general introduction to linking is <ahref="http://www.lurklurk.org/linkers/linkers.html">Beginner's Guide to Linkers</a> by David Drysdale.</p>
<h2><aclass="anchor"id="build_link_win32"></a>
With MinGW or Visual C++ on Windows</h2>
<p>The static version of the GLFW library is named <code>glfw3</code>. When using this version, it is also necessary to link with some libraries that GLFW uses.</p>
<p>When linking an application under Windows that uses the static version of GLFW, you must link with <code>opengl32</code>. On some versions of MinGW, you must also explicitly link with <code>gdi32</code>, while other versions of MinGW include it in the set of default libraries along with other dependencies like <code>user32</code> and <code>kernel32</code>. If you are using GLU, you must also link with <code>glu32</code>.</p>
<p>The link library for the GLFW DLL is named <code>glfw3dll</code>. When compiling an application that uses the DLL version of GLFW, you need to define the <code>GLFW_DLL</code> macro <em>before</em> any inclusion of the GLFW header. This can be done either with a compiler switch or by defining it in your source code.</p>
<p>An application using the GLFW DLL does not need to link against any of its dependencies, but you still have to link against <code>opengl32</code> if your application uses OpenGL and <code>glu32</code> if it uses GLU.</p>
<p>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 <aclass="el"href="build_guide.html#build_link_cmake_package">With CMake and installed GLFW binaries</a>.</p>
<p>With just a few changes to your <code>CMakeLists.txt</code> you can have the GLFW source tree built along with your application.</p>
<p>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.</p>
<divclass="fragment"><divclass="line"><spanclass="keyword">set</span>(GLFW_BUILD_DOCS OFF CACHE BOOL <spanclass="stringliteral">""</span> FORCE)</div><divclass="line"><spanclass="keyword">set</span>(GLFW_BUILD_TESTS OFF CACHE BOOL <spanclass="stringliteral">""</span> FORCE)</div><divclass="line"><spanclass="keyword">set</span>(GLFW_BUILD_EXAMPLES OFF CACHE BOOL <spanclass="stringliteral">""</span> FORCE)</div></div><!-- fragment --><p>Then add the root directory of the GLFW source tree to your project. This will add the <code>glfw</code> target and the necessary cache variables to your project.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> add_subdirectory(path/to/glfw)</div></div><!-- fragment --><p>Once GLFW has been added to the project, link against it with the <code>glfw</code> 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 <aclass="el"href="build_guide.html#build_macros">GLFW_DLL</a> macro.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> target_link_libraries(myapp glfw)</div></div><!-- fragment --><p>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 <aclass="el"href="context_guide.html#context_glext_auto">extension loader library</a> you can find it by requiring the OpenGL package.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> find_package(OpenGL REQUIRED)</div></div><!-- fragment --><p>If OpenGL is found, the <code>OPENGL_FOUND</code> variable is true and the <code>OPENGL_INCLUDE_DIR</code> and <code>OPENGL_gl_LIBRARY</code> cache variables can be used.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> target_include_directories(myapp ${OPENGL_INCLUDE_DIR})</div><divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span> target_link_libraries(myapp ${OPENGL_gl_LIBRARY})</div></div><!-- fragment --><p>The OpenGL CMake package also looks for GLU. If GLU is found, the <code>OPENGL_GLU_FOUND</code> variable is true and the <code>OPENGL_INCLUDE_DIR</code> and <code>OPENGL_glu_LIBRARY</code> cache variables can be used.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> target_link_libraries(myapp ${OPENGL_glu_LIBRARY})</div></div><!-- fragment --><dlclass="section note"><dt>Note</dt><dd>GLU has been deprecated and should not be used in new code, but some legacy code requires it.</dd></dl>
<p>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 <aclass="el"href="build_guide.html#build_link_cmake_source">With CMake and GLFW source</a>.</p>
<p>With just a few changes to your <code>CMakeLists.txt</code>, you can locate the package and target files generated when GLFW is installed.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> find_package(glfw3 3.2 REQUIRED)</div></div><!-- fragment --><p>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 <aclass="el"href="context_guide.html#context_glext_auto">extension loader library</a> you can find it by requiring the OpenGL package.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> find_package(OpenGL REQUIRED)</div></div><!-- fragment --><p>If OpenGL is found, the <code>OPENGL_FOUND</code> variable is true and the <code>OPENGL_INCLUDE_DIR</code> and <code>OPENGL_gl_LIBRARY</code> cache variables can be used.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> target_include_directories(myapp ${OPENGL_INCLUDE_DIR})</div><divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span> target_link_libraries(myapp ${OPENGL_gl_LIBRARY})</div></div><!-- fragment --><p>The OpenGL CMake package also looks for GLU. If GLU is found, the <code>OPENGL_GLU_FOUND</code> variable is true and the <code>OPENGL_INCLUDE_DIR</code> and <code>OPENGL_glu_LIBRARY</code> cache variables can be used.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> target_link_libraries(myapp ${OPENGL_glu_LIBRARY})</div></div><!-- fragment --><dlclass="section note"><dt>Note</dt><dd>GLU has been deprecated and should not be used in new code, but some legacy code requires it.</dd></dl>
<p>GLFW supports <ahref="http://www.freedesktop.org/wiki/Software/pkg-config/">pkg-config</a>, and the <code>glfw3.pc</code> pkg-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.</p>
<p>A typical compile and link command-line when using the static version of the GLFW library may look like this:</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --static --libs glfw3`</div></div><!-- fragment --><p>If you are using the shared version of the GLFW library, simply omit the <code>--static</code> flag.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`</div></div><!-- fragment --><p>You can also use the <code>glfw3.pc</code> file without installing it first, by using the <code>PKG_CONFIG_PATH</code> environment variable.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> env PKG_CONFIG_PATH=path/to/glfw/src cc `pkg-config --cflags glfw3` -o myprog myprog.c `pkg-config --libs glfw3`</div></div><!-- fragment --><p>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 OS X, GLU is built into the OpenGL framework, so if you need GLU you don't need to do anything extra. If you need GLU and are using Linux or BSD, you should add the <code>glu</code> pkg-config package.</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --libs glfw3 glu`</div></div><!-- fragment --><dlclass="section note"><dt>Note</dt><dd>GLU has been deprecated and should not be used in new code, but some legacy code requires it.</dd></dl>
<p>If you are using the static version of the GLFW library, make sure you don't link statically against GLU.</p>
<p>If you are using the dynamic library version of GLFW, simply add it to the project dependencies.</p>
<p>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 <code>/System/Library/Frameworks</code>.</p>
<h2><aclass="anchor"id="build_link_osx"></a>
With command-line on OS X</h2>
<p>It is recommended that you use <aclass="el"href="build_guide.html#build_link_pkgconfig">pkg-config</a> 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 <code>-l</code> and <code>-framework</code> switches.</p>
<p>If you are using the dynamic GLFW library, which is named <code>libglfw.3.dylib</code>, do:</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> cc -o myprog myprog.c -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo</div></div><!-- fragment --><p>If you are using the static library, named <code>libglfw3.a</code>, substitute <code>-lglfw3</code> for <code>-lglfw</code>.</p>
<p>Note that you do not add the <code>.framework</code> extension to a framework when linking against it from the command-line.</p>
<p>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 <code>libGL</code>-style OpenGL libraries, they are for use with the X Window System and will <em>not</em> work with the OS X native version of GLFW. </p>