<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Luis Omar Alvarez Mures on Medium]]></title>
        <description><![CDATA[Stories by Luis Omar Alvarez Mures on Medium]]></description>
        <link>https://medium.com/@omaralv?source=rss-b10cfca762b5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Zf0Y0DxUOIB_rjkHO6OBgg.jpeg</url>
            <title>Stories by Luis Omar Alvarez Mures on Medium</title>
            <link>https://medium.com/@omaralv?source=rss-b10cfca762b5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 20:32:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@omaralv/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating a Shared Library with Modern CMake]]></title>
            <link>https://medium.com/@omaralv/creating-a-shared-library-with-modern-cmake-f332a69bb478?source=rss-b10cfca762b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f332a69bb478</guid>
            <category><![CDATA[cmake]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Luis Omar Alvarez Mures]]></dc:creator>
            <pubDate>Mon, 16 Apr 2018 18:13:28 GMT</pubDate>
            <atom:updated>2018-10-12T13:42:44.000Z</atom:updated>
            <content:encoded><![CDATA[<p>After several days wondering why CMake has to be so dark and complicated, I came across several talks about modern CMake. In these talks I finally started to see how modern CMake can be easy to use and make things like creating a shared library a lot more simple. So what’s this all about? Easiest way for me to show you, is to show step by step how to create it.</p><h3>Directory Layout</h3><p>We want to keep things tidy, so we will have a top-level src directory with the source files. That way I can make a include directory next to it that holds all my header files.</p><pre>.<br>├── CMakeLists.txt<br>├── include<br>│   ├── class1.hpp<br>│   ├── ...<br>│   └── class2.hpp<br>└── src<br>    ├── class1.cpp<br>    ├── ...<br>    └── class2.cpp</pre><h3>Top-level CMakeLists.txt</h3><p>We will have a lot going on here. First, we need to tell CMake the library name, version and language. Next, we set the default build type, just in case the user does not specify it. We also will set the source file variable and instruct CMake to keep them private and include the proper directories. Last but not least, we create output targets and set the install paths where the library and config file will be installed.</p><pre>cmake_minimum_required(VERSION 3.5)<br>project(mylib VERSION 1.0.0 LANGUAGES CXX)<br><br>set(DEFAULT_BUILD_TYPE &quot;Release&quot;)</pre><pre>if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)<br>  message(STATUS &quot;Setting build type to &#39;${DEFAULT_BUILD_TYPE}&#39; as none was specified.&quot;)<br>  set(CMAKE_BUILD_TYPE &quot;${DEFAULT_BUILD_TYPE}&quot; CACHE STRING &quot;Choose the type of build.&quot; FORCE)<br>  # Set the possible values of build type for cmake-gui<br>  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS &quot;Debug&quot; &quot;Release&quot; &quot;MinSizeRel&quot; &quot;RelWithDebInfo&quot;)<br>endif()<br><br>include(GNUInstallDirs)<br><br>set(SOURCE_FILES src/class1.cpp src/class2.cpp)</pre><pre>add_library(${PROJECT_NAME})</pre><pre>target_include_directories(${PROJECT_NAME} PUBLIC<br>    $&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include&gt;<br>    $&lt;INSTALL_INTERFACE:include&gt;<br>    PRIVATE src)<br><br>set_target_properties(${PROJECT_NAME} PROPERTIES<br>    VERSION ${PROJECT_VERSION}<br>    SOVERSION 1)<br><br>install(TARGETS ${PROJECT_NAME} EXPORT MyLibConfig<br>    ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}<br>    LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}<br>    RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})<br>install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})<br><br>install(EXPORT MyLibConfig DESTINATION share/MyLib/cmake)<br><br>export(TARGETS ${PROJECT_NAME} FILE MyLibConfig.cmake)</pre><h3>Importing Our Library</h3><p>Once we have compiled our library, we will be able to use the following snippet to use it in another project.</p><pre>find_package(MyLib REQUIRED)<br><br># No need to perform include_directories(...)<br>target_link_libraries(${TARGET} mylib)</pre><p>That’s it, if it has been installed in a standard directory it will be found and there is no need to do anything else. If it has been installed in a non-standard path, it is also easy, just tell CMake where to find MyLibConfig.cmake using the next snippet.</p><pre>cmake -DMyLib_DIR=/non/standard/install/path ..</pre><p>I hope this helps everybody as much as it has helped me. Old ways of doing this were quite cumbersome and lead to problems finding our library.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f332a69bb478" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>