<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Edgar Luque - cmake</title>
    <subtitle>Software Developer</subtitle>
    <link rel="self" type="application/atom+xml" href="https://edgl.dev/categories/cmake/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://edgl.dev"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2020-11-12T00:00:00+00:00</updated>
    <id>https://edgl.dev/categories/cmake/atom.xml</id>
    <entry xml:lang="en">
        <title>Creating precompiled headers with cmake</title>
        <published>2020-11-12T00:00:00+00:00</published>
        <updated>2020-11-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Edgar Luque
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://edgl.dev/blog/cmake-precompiled-headers/"/>
        <id>https://edgl.dev/blog/cmake-precompiled-headers/</id>
        
        <content type="html" xml:base="https://edgl.dev/blog/cmake-precompiled-headers/">&lt;h2 id=&quot;what-are-precompiled-headers&quot;&gt;What are precompiled headers?&lt;&#x2F;h2&gt;
&lt;p&gt;They are a partially processed version of header files, this speeds up compilation because it doesn&#x27;t have to repeatedly parse the original header.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;how-to-use-it&quot;&gt;How to use it&lt;&#x2F;h2&gt;
&lt;p&gt;The way to do it is to pass the header files you want precompiled to the &lt;code&gt;target_precompile_headers&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine this folder structure:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── CMakeLists.txt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── src&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── header.h&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    └── main.cpp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1 directory, 3 files&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we create a very basic CMakeLists.txt:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;cmake_minimum_required&lt;&#x2F;span&gt;&lt;span&gt;(VERSION 3.16)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;project&lt;&#x2F;span&gt;&lt;span&gt;(MyProject)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(HEADER_FILES&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	src&#x2F;header.h)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(SOURCE_FILES&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	src&#x2F;main.cpp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;	${HEADER_FILES}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;include_directories&lt;&#x2F;span&gt;&lt;span&gt;(src)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;add_executable&lt;&#x2F;span&gt;&lt;span&gt;(MyProject &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;${SOURCE_FILES}&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice we require a minimum cmake version of 3.16, this is due to &lt;code&gt;target_precompile_headers&lt;&#x2F;code&gt; being added in that version.&lt;&#x2F;p&gt;
&lt;p&gt;We add the command after the &lt;code&gt;add_executable&lt;&#x2F;code&gt; instruction to the private scope.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s recommended to use the private scope to prevent precompiled headers appearing in an installed target, consumers should decide whether they want to use precompiled headers or not.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;target_precompile_headers(MyProject PRIVATE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; ${HEADER_FILES}&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When compiling you can see now that it indeed compiles the headers:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Scanning dependencies of target MyProject&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[ 33%] Building CXX object CMakeFiles&#x2F;MyProject.dir&#x2F;cmake_pch.hxx.gch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[ 66%] Building CXX object CMakeFiles&#x2F;MyProject.dir&#x2F;src&#x2F;main.cpp.o&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[100%] Linking CXX executable MyProject&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[100%] Built target MyProject&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Setting Up SDL2 with CMake</title>
        <published>2019-04-27T00:00:00+00:00</published>
        <updated>2019-04-27T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Edgar Luque
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://edgl.dev/blog/sdl2-cmake/"/>
        <id>https://edgl.dev/blog/sdl2-cmake/</id>
        
        <content type="html" xml:base="https://edgl.dev/blog/sdl2-cmake/">&lt;h2 id=&quot;installing-cmake&quot;&gt;Installing CMake&lt;&#x2F;h2&gt;
&lt;p&gt;Most common distributions have cmake available on their package manager repostories:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# Debian based&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; apt install cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# Arch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;pacman&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -S&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;install-sdl2-libraries&quot;&gt;Install SDL2 libraries&lt;&#x2F;h2&gt;
&lt;p&gt;I only know about the debian based ones, if you are on another distro you should look them up.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; apt install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-net-dev libsdl2-ttf-dev libsdl2-gfx-dev&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;directory-structure&quot;&gt;Directory Structure&lt;&#x2F;h2&gt;
&lt;p&gt;Here is a common directory structure when using cmake to find packages:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;│   ├── FindSDL2.cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;│   ├── FindSDL2_mixer.cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;│   └── FindSDL2_net.cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── CMakeLists.txt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── src&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    └── main.cpp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can find the cmake files to find SDL2 and it&#x27;s components
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;aminosbh&#x2F;sdl2-cmake-modules&quot;&gt;here&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Or you can use this simple command:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;wget&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;aminosbh&#x2F;sdl2-cmake-modules&#x2F;master&#x2F;FindSDL2{,_gfx,_image,_mixer,_net,_ttf}.cmake&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;creating-the-cmakelists-txt-file&quot;&gt;Creating the CMakeLists.txt file&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;cmake_minimum_required&lt;&#x2F;span&gt;&lt;span&gt;(VERSION 3.13)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;project&lt;&#x2F;span&gt;&lt;span&gt;(MyProject)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# Needed so that cmake uses our find modules.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(APPEND CMAKE_MODULE_PATH&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; ${CMAKE_CURRENT_SOURCE_DIR}&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;cmake)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;find_package&lt;&#x2F;span&gt;&lt;span&gt;(SDL2 REQUIRED)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;find_package&lt;&#x2F;span&gt;&lt;span&gt;(SDL2_net REQUIRED)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;find_package&lt;&#x2F;span&gt;&lt;span&gt;(SDL2_mixer REQUIRED)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;find_package&lt;&#x2F;span&gt;&lt;span&gt;(SDL2_image REQUIRED)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;find_package&lt;&#x2F;span&gt;&lt;span&gt;(SDL2_gfx REQUIRED)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;find_package&lt;&#x2F;span&gt;&lt;span&gt;(SDL2_ttf REQUIRED)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(SOURCE_FILES&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    src&#x2F;main.cpp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;include_directories&lt;&#x2F;span&gt;&lt;span&gt;(src)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;add_executable&lt;&#x2F;span&gt;&lt;span&gt;(MyProject &lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;${SOURCE_FILES}&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;target_link_libraries&lt;&#x2F;span&gt;&lt;span&gt;(MyProject SDL2::Main SDL2::Net SDL2::Mixer SDL2::Image SDL2::TTF SDL2::GFX)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And thats it! Now you can remove the SDL2 components you don&#x27;t want to use.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;building&quot;&gt;Building&lt;&#x2F;h2&gt;
&lt;p&gt;Following the standard cmake procedure:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;mkdir&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; build&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; build&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;cmake&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; ..&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;make&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -j$&lt;&#x2F;span&gt;&lt;span&gt;{nproc}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
</feed>
