<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged cmake</title>
<link>https://coderlegion.com/tag/cmake</link>
<description>Powered by Question2Answer</description>
<item>
<title>Tuesday Coding Tip 23 — CMake configure_file</title>
<link>https://coderlegion.com/26342/tuesday-coding-tip-23-cmake-configurefile</link>
<description>&lt;p&gt;Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;In unit tests, I often don’t want to bother with the working folder of the test binary. But if those tests need physical files from the disc, I need to know their absolute paths (which are different on each machine). Luckily, CMake has a function &lt;code&gt;configure_file&lt;/code&gt; that can write CMake variables to a file template.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cmake&quot;&gt;cmake_minimum_required ( VERSION 3.10 )

set ( USE_MSVC_FEATURES (&quot;${CMAKE_GENERATOR}&quot; MATCHES &quot;Visual Studio 17 2022&quot;) )

project ( DEMO CXX )

# CMakeLists.txt
configure_file(
    &quot;${CMAKE_SOURCE_DIR}/TemplateHeader.hpp.in&quot; # input
    &quot;${CMAKE_SOURCE_DIR}/FinalHeader.hpp&quot; # output
)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, I can prepare a template for a header that will hold an absolute path to a folder with helper files, and during configuration, I can produce a valid C++ header with the path filled out. CMake will replace variables escaped as &lt;code&gt;@VAR@&lt;/code&gt;, &lt;code&gt;${VAR}&lt;/code&gt;, &lt;code&gt;$CACHE{VAR}&lt;/code&gt; or &lt;code&gt;$ENV{VAR}&lt;/code&gt; and furthermore it can do things like generating &lt;code&gt;#define&lt;/code&gt; directives.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;// TemplateHeader.hpp.in
#pragma once

#include &amp;lt;filesystem&amp;gt;

#cmakedefine01 USE_MSVC_FEATURES

const std::filesystem::path TESTDATA_FOLDER = &quot;@CMAKE_CURRENT_SOURCE_DIR@/TestFiles&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;// Generated FinalHeader.hpp
#pragma once

#include &amp;lt;filesystem&amp;gt;

#define USE_MSVC_FEATURES 1

const std::filesystem::path TESTDATA_FOLDER = &quot;C:/Work/Project/TestFiles&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Full documentation: &lt;a rel=&quot;nofollow&quot; href=&quot;https://cmake.org/cmake/help/latest/command/configure_file.html&quot;&gt;https://cmake.org/cmake/help/latest/command/configure_file.html&lt;/a&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/26342/tuesday-coding-tip-23-cmake-configurefile</guid>
<pubDate>Tue, 22 Sep 2026 09:59:00 +0000</pubDate>
</item>
<item>
<title>Tuesday Coding Tip 10— CTest</title>
<link>https://coderlegion.com/15918/tuesday-coding-tip-10-ctest</link>
<description>&lt;p&gt;Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;CMake can do many things and instrumenting your test suite is one of them. Use &lt;code&gt;add_test&lt;/code&gt; to tell cmake it should run a particular command with given parameters. If it returns zero, it succeeded. You can use it to simply run a testrunner binary or to make a matrix of input parameters that are expected to not crash the program.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;cmake_minimum_required ( VERSION 3.26 )

project ( testrunner )
add_executable ( ${PROJECT_NAME} Main.cpp )

enable_testing()
add_test(
   NAME &quot;Example with no params&quot;
   COMMAND ${PROJECT_NAME}
)

add_test(
   NAME &quot;Example with help param&quot;
   COMMAND ${PROJECT_NAME} -h
)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to &lt;code&gt;add_test&lt;/code&gt; do anything, one has to also call &lt;code&gt;enable_testing()&lt;/code&gt;. Running the tests is just a matter of running ctest from command line. Just note that ctest requires you to specify a build configuration if you have more than one.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=4332370757284522768&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/15918/tuesday-coding-tip-10-ctest</guid>
<pubDate>Tue, 05 May 2026 09:59:00 +0000</pubDate>
</item>
</channel>
</rss>