<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GLFW: Building applications</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
	<div class="glfwheader">
		<a href="http://www.glfw.org/" id="glfwhome">GLFW</a>
		<ul class="glfwnavbar">
			<li><a href="http://www.glfw.org/documentation.html">Documentation</a></li>
			<li><a href="http://www.glfw.org/download.html">Download</a></li>
			<li><a href="http://www.glfw.org/media.html">Media</a></li>
			<li><a href="http://www.glfw.org/community.html">Community</a></li>
		</ul>
	</div>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Building applications </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#build_include">Including the GLFW header file</a><ul><li class="level2"><a href="#build_macros">GLFW header option macros</a></li>
</ul>
</li>
<li class="level1"><a href="#build_link">Link with the right libraries</a><ul><li class="level2"><a href="#build_link_win32">With MinGW or Visual C++ on Windows</a></li>
<li class="level2"><a href="#build_link_cmake_source">With CMake and GLFW source</a></li>
<li class="level2"><a href="#build_link_cmake_package">With CMake and installed GLFW binaries</a></li>
<li class="level2"><a href="#build_link_pkgconfig">With makefiles and pkg-config on Unix</a></li>
<li class="level2"><a href="#build_link_xcode">With Xcode on OS X</a></li>
<li class="level2"><a href="#build_link_osx">With command-line on OS X</a></li>
</ul>
</li>
</ul>
</div>
<div class="textblock"><p>This is about compiling and linking applications that use GLFW. For information on how to write such applications, start with the <a class="el" href="quick_guide.html">introductory tutorial</a>. For information on how to compile the GLFW library itself, see <a class="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><a class="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>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="glfw3_8h.html">GLFW/glfw3.h</a>&gt;</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 <a href="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><a class="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>
<dl class="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><a class="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 <a href="http://www.lurklurk.org/linkers/linkers.html">Beginner's Guide to Linkers</a> by David Drysdale.</p>
<h2><a class="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>
<h2><a class="anchor" id="build_link_cmake_source"></a>
With CMake and GLFW source</h2>
<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 <a class="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>
<div class="fragment"><div class="line"><span class="keyword">set</span>(GLFW_BUILD_DOCS OFF CACHE BOOL <span class="stringliteral">&quot;&quot;</span> FORCE)</div><div class="line"><span class="keyword">set</span>(GLFW_BUILD_TESTS OFF CACHE BOOL <span class="stringliteral">&quot;&quot;</span> FORCE)</div><div class="line"><span class="keyword">set</span>(GLFW_BUILD_EXAMPLES OFF CACHE BOOL <span class="stringliteral">&quot;&quot;</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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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 <a class="el" href="build_guide.html#build_macros">GLFW_DLL</a> macro.</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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 <a class="el" href="context_guide.html#context_glext_auto">extension loader library</a> you can find it by requiring the OpenGL package.</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;target_include_directories(myapp ${OPENGL_INCLUDE_DIR})</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;target_link_libraries(myapp ${OPENGL_glu_LIBRARY})</div></div><!-- fragment --><dl class="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>
<h2><a class="anchor" id="build_link_cmake_package"></a>
With CMake and installed GLFW binaries</h2>
<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 <a class="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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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 <a class="el" href="context_guide.html#context_glext_auto">extension loader library</a> you can find it by requiring the OpenGL package.</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;target_include_directories(myapp ${OPENGL_INCLUDE_DIR})</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;target_link_libraries(myapp ${OPENGL_glu_LIBRARY})</div></div><!-- fragment --><dl class="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>
<h2><a class="anchor" id="build_link_pkgconfig"></a>
With makefiles and pkg-config on Unix</h2>
<p>GLFW supports <a href="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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --libs glfw3 glu`</div></div><!-- fragment --><dl class="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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;cc `pkg-config --cflags glfw3 glu` -o myprog myprog.c `pkg-config --static --libs glfw3` `pkg-config --libs glu`</div></div><!-- fragment --><h2><a class="anchor" id="build_link_xcode"></a>
With Xcode on OS X</h2>
<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><a class="anchor" id="build_link_osx"></a>
With command-line on OS X</h2>
<p>It is recommended that you use <a class="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>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;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>
</div></div><!-- contents -->
<address class="footer">
<p>
Last update on Thu Aug 18 2016 for GLFW 3.2.1
</p>
</address>
</body>
</html>
