{"id":29,"date":"2013-07-14T21:32:36","date_gmt":"2013-07-15T02:32:36","guid":{"rendered":"https:\/\/104.236.124.184\/cmake-tutorial-5-functionally-improved-testing.html"},"modified":"2013-07-14T21:32:36","modified_gmt":"2013-07-15T02:32:36","slug":"cmake-tutorial-5-functionally-improved-testing","status":"publish","type":"post","link":"https:\/\/www.johnlamp.net\/?p=29","title":{"rendered":"CMake Tutorial &#8211; Chapter&#160;5: Functionally Improved Testing"},"content":{"rendered":"<nav class=\"contents\">\n<h1>Contents<\/h1>\n<ol>\n<li><a href=\"#section-Introduction\">Introduction<\/a><\/li>\n<li><a href=\"#section-ASimpleFunction\">A Simple Function<\/a><\/li>\n<li>\n<ol>\n<li><a href=\"#section-CommandsAndFunctionsAndMacros!OhMy!\">Commands and Functions and Macros! Oh my!<\/a><\/li>\n<li><a href=\"#section-Scope\">Scope<\/a><\/li>\n<li>\n<ol>\n<li><a href=\"#section-LocalScope\">Local Scope<\/a><\/li>\n<li><a href=\"#section-ParentScope\">Parent Scope<\/a><\/li>\n<li><a href=\"#section-DirectoryScope\">Directory Scope<\/a><\/li>\n<li><a href=\"#section-GlobalScope\">Global Scope<\/a><\/li>\n<li><a href=\"#section-CacheScope\">Cache Scope<\/a><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<li><a href=\"#section-Let'SIncludeSomeOrganization\">Let&#8217;s Include Some Organization<\/a><\/li>\n<li><a href=\"#section-Lists!\">Lists!<\/a><\/li>\n<li><a href=\"#section-AutoPlay\">Auto Play<\/a><\/li>\n<\/ol>\n<\/nav>\n<section>\n<h1 id=\"section-Introduction\">Introduction<\/h1>\n<p>\n    Last time we added a nice unit test and then set up CMake to build it, of<br \/>\n    course, and add it to the list of tests that CTest will run. This is great,<br \/>\n    now we can run <code>cmake<\/code> then use <kbd>make<\/kbd> and <kbd>make<br \/>\n    test<\/kbd> to test our project. Now it&#8217;s time to build on our success<br \/>\n    because we certainly aren&#8217;t done yet.\n  <\/p>\n<p>\n    The main problem we need to tackle is that there are currently 3 steps to<br \/>\n    creating a test program:\n  <\/p>\n<ol>\n<li>\n      add the executable target\n    <\/li>\n<li>\n      link the executable against the &#8220;gmock_main&#8221; library\n    <\/li>\n<li>\n      add the test to CTest&#8217;s list of tests\n    <\/li>\n<\/ol>\n<p>\n    That&#8217;s 3 steps too many. If you are thinking that 3 steps aren&#8217;t too many<br \/>\n    remember that any project of a useful size will have a rather large number<br \/>\n    of unit tests, each of which will require these same 3 steps &ndash; that&#8217;s<br \/>\n    a lot of repetition. As programmers we should not repeat ourselves, and we<br \/>\n    shouldn&#8217;t slack off just because we are <span class=\"strike\">merely<\/span><br \/>\n    setting up our build system. What we want is the ability to add a new test<br \/>\n    in a single step. Writing the test is hard enough, building and running it<br \/>\n    should be easy.\n  <\/p>\n<p>\n    Lucky for us CMake offers the ability to write functions. So we will start<br \/>\n    by writing a function that combines these 3 steps so that only one step will<br \/>\n    be needed. Once we have the function we will improve it further taking<br \/>\n    advantage of the fact that we will only have to write said improvements<br \/>\n    once.\n  <\/p>\n<\/section>\n<section>\n<h1 id=\"section-ASimpleFunction\">A Simple Function<\/h1>\n<p>\n    We have 3 simple steps to encapsulate in a function, that should be simple,<br \/>\n    right?\n  <\/p>\n<section class=\"code cmake\">\n<header class=\"clear-after\">\n<h1>ToDoCore\/unit_test\/CMakeLists.txt<\/h1>\n<div class=\"hll legend\">New or modified lines in bold.<\/div>\n<\/header>\n<div class=\"highlight\">\n<pre><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-1\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-1\"><\/a><span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">GMOCK_DIR<\/span> <span class=\"s2\">&quot;..\/..\/..\/..\/..\/gmock\/gmock-1.6.0&quot;<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-2\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-2\"><\/a>    <span class=\"s\">CACHE<\/span> <span class=\"s\">PATH<\/span> <span class=\"s2\">&quot;The path to the GoogleMock test framework.&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-3\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-3\"><\/a>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-4\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-4\"><\/a><span class=\"nb\">if<\/span> <span class=\"p\">(<\/span><span class=\"s2\">&quot;${CMAKE_CXX_COMPILER_ID}&quot;<\/span> <span class=\"s\">STREQUAL<\/span> <span class=\"s2\">&quot;MSVC&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-5\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-5\"><\/a>    <span class=\"c\"># force this option to ON so that Google Test will use \/MD instead of \/MT<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-6\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-6\"><\/a>    <span class=\"c\"># \/MD is now the default for Visual Studio, so it should be our default, too<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-7\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-7\"><\/a>    <span class=\"nb\">option<\/span><span class=\"p\">(<\/span><span class=\"s\">gtest_force_shared_crt<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-8\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-8\"><\/a>           <span class=\"s2\">&quot;Use shared (DLL) run-time lib even when Google Test is built as static lib.&quot;<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-9\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-9\"><\/a>           <span class=\"s\">ON<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-10\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-10\"><\/a><span class=\"nb\">elseif<\/span> <span class=\"p\">(<\/span><span class=\"s\">APPLE<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-11\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-11\"><\/a>    <span class=\"nb\">add_definitions<\/span><span class=\"p\">(<\/span><span class=\"s\">-DGTEST_USE_OWN_TR1_TUPLE=1<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-12\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-12\"><\/a><span class=\"nb\">endif<\/span><span class=\"p\">()<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-13\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-13\"><\/a><span class=\"nb\">add_subdirectory<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">CMAKE_BINARY_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/gmock<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-14\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-14\"><\/a><span class=\"nb\">set_property<\/span><span class=\"p\">(<\/span><span class=\"s\">TARGET<\/span> <span class=\"s\">gtest<\/span> <span class=\"s\">APPEND_STRING<\/span> <span class=\"s\">PROPERTY<\/span> <span class=\"s\">COMPILE_FLAGS<\/span> <span class=\"s2\">&quot; -w&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-15\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-15\"><\/a>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-16\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-16\"><\/a><span class=\"nb\">include_directories<\/span><span class=\"p\">(<\/span><span class=\"s\">SYSTEM<\/span> <span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/gtest\/include<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-17\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-17\"><\/a>                           <span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/include<\/span><span class=\"p\">)<\/span>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-18\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-18\"><\/a>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-19\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-19\"><\/a>\n<a id=\"ToDoCore\/unit_test\/CMakeLists.txt-20\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-20\"><\/a><span class=\"hll\"><span class=\"c\">#<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-21\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-21\"><\/a><span class=\"hll\"><span class=\"c\"># add_gmock_test(&lt;target&gt; &lt;sources&gt;...)<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-22\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-22\"><\/a><span class=\"hll\"><span class=\"c\">#<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-23\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-23\"><\/a><span class=\"hll\"><span class=\"c\">#  Adds a Google Mock based test executable, &lt;target&gt;, built from &lt;sources&gt; and<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-24\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-24\"><\/a><span class=\"hll\"><span class=\"c\">#  adds the test so that CTest will run it. Both the executable and the test<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-25\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-25\"><\/a><span class=\"hll\"><span class=\"c\">#  will be named &lt;target&gt;.<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-26\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-26\"><\/a><span class=\"hll\"><span class=\"c\">#<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-27\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-27\"><\/a><span class=\"hll\"><span class=\"nb\">function<\/span><span class=\"p\">(<\/span><span class=\"s\">add_gmock_test<\/span> <span class=\"s\">target<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-28\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-28\"><\/a><span class=\"hll\">    <span class=\"nb\">add_executable<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">ARGN<\/span><span class=\"o\">}<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-29\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-29\"><\/a><span class=\"hll\">    <span class=\"nb\">target_link_libraries<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"s\">gmock_main<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-30\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-30\"><\/a><span class=\"hll\">\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-31\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-31\"><\/a><span class=\"hll\">    <span class=\"nb\">add_test<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-32\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-32\"><\/a><span class=\"hll\"><span class=\"nb\">endfunction<\/span><span class=\"p\">()<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-33\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-33\"><\/a><span class=\"hll\">\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-34\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-34\"><\/a><span class=\"hll\">\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-35\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-35\"><\/a><span class=\"hll\"><span class=\"nb\">add_gmock_test<\/span><span class=\"p\">(<\/span><span class=\"s\">ToDoTest<\/span> <span class=\"s\">ToDoTest.cc<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"ToDoCore\/unit_test\/CMakeLists.txt-36\" class=\"line-number\" href=\"#ToDoCore\/unit_test\/CMakeLists.txt-36\"><\/a><span class=\"hll\"><span class=\"nb\">target_link_libraries<\/span><span class=\"p\">(<\/span><span class=\"s\">ToDoTest<\/span> <span class=\"s\">toDoCore<\/span><span class=\"p\">)<\/span>\n<\/span><\/pre>\n<\/div>\n<\/section>\n<p>  <a class=\"sources\" href=\"https:\/\/www.johnlamp.net\/sources\/chapter5-1.zip\"><img src=\"https:\/\/www.johnlamp.net\/images\/zip.png\" alt=\"[zip file] \" \/>Source<\/a><\/p>\n<dl>\n<dd>\n      I like to put comments before my functions that show how they should be<br \/>\n      called and explain what they do.\n    <\/dd>\n<dt class=\"code\">\n      function(add_gmock_test target)\n    <\/dt>\n<dd>\n      Start the definition of the function <code>add_gmock_test<\/code> with one<br \/>\n      required parameter <code>target<\/code>.\n    <\/dd>\n<dd>\n      Inside the function its first argument is available as the variable<br \/>\n      <code>target<\/code> and the rest of the arguments are available in a list<br \/>\n      stored in the variable <code>ARGN<\/code>. CMake will allow you to pass<br \/>\n      more arguments to a function than the number of parameters it defined. It<br \/>\n      is up to the writer of the function to handle all of them, validate them<br \/>\n      and produce an error if they aren&#8217;t correct, or merely ignore them. In<br \/>\n      this case we are just passing them all on to the command<br \/>\n      <code>add_executable()<\/code>.\n    <\/dd>\n<dd>\n      Also available is the variable <code>ARGC<\/code> which holds the count of<br \/>\n      all arguments passed to the function, both ones matching parameters and<br \/>\n      any extras. Additionally each argument can be accessed via the variables<br \/>\n      <code>ARGV0<\/code>, <code>ARGV1<\/code>, &#8230;<br \/>\n      <code>ARGV<span class=\"arg\">N<\/span><\/code>. As if that weren&#8217;t enough<br \/>\n      ways to access function arguments all arguments are also available as a<br \/>\n      list stored in the variable <code>ARGV<\/code>. This affords a lot of<br \/>\n      flexibility but can make argument validation and handling difficult.\n    <\/dd>\n<dd>\n      <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:function\"><code>function()<\/code> documentation<\/a>      <time class=\"access\" datetime=\"2013-06-01\">(2013-06-01)<\/time>    <\/dd>\n<dt class=\"code\">\n      endfunction()\n    <\/dt>\n<dd>\n      Ends the definition of a function. As I&#8217;ve said before CMake&#8217;s syntax is a<br \/>\n      bit strange. You can pass the name of the function as an argument to this<br \/>\n      command, but it is not required. If you do it should match otherwise CMake<br \/>\n      will print a warning when configuring. I think it&#8217;s easier to read if no<br \/>\n      arguments are passed to <code>endfunction()<\/code> and functions shouldn&#8217;t<br \/>\n      be long enough that a reminder of what function is being ended is needed.\n    <\/dd>\n<dd>\n      <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:endfunction\"><code>endfunction()<\/code> documentation<\/a>      <time class=\"access\" datetime=\"2013-06-01\">(2013-06-01)<\/time>    <\/dd>\n<dt class=\"code\">\n      add_gmock_test(ToDoTest ToDoTest.cc)\n    <\/dt>\n<dd>\n      Now we use the function we just wrote to add our Google Mock based<br \/>\n      test. With the function written it is now much simpler as we don&#8217;t need to<br \/>\n      write out the three separate commands every time.\n    <\/dd>\n<dt class=\"code\">\n      target_link_libraries(ToDoTest toDoCore)\n    <\/dt>\n<dd>\n      We still have to link our test with the &#8220;toDoCore&#8221; library. Since this is<br \/>\n      specific to this test and not all tests it wouldn&#8217;t make sense to include<br \/>\n      this in our function.\n    <\/dd>\n<\/dl>\n<h2 id=\"section-CommandsAndFunctionsAndMacros!OhMy!\">Commands and Functions and Macros! Oh my!<\/h2>\n<p>\n    So far we have seen several CMake commands and now even written a function!<br \/>\n    You may wonder what the difference is between a command and a<br \/>\n    function. Simply put commands are built into CMake and functions are written<br \/>\n    using CMake&#8217;s language. While some commands behave quite similarly to<br \/>\n    functions, e.g. <code>add_executable<\/code>, some others behave in ways that<br \/>\n    cannot be mimicked using functions or macros, e.g. <code>if()<\/code> and<br \/>\n    <code>function()<\/code>.\n  <\/p>\n<p>\n    Macros, on the other hand, are similar to functions in that they are written<br \/>\n    the same and offer all of the same ways for accessing arguments. However,<br \/>\n    macros don&#8217;t have their own scope and rather than dereferencing arguments<br \/>\n    when run arguments are replaced instead. The first difference is what makes<br \/>\n    macros both useful and dangerous, the second is more subtle and can make<br \/>\n    working with lists difficult. <span class=\"subtle\">(Yes, I know. I haven&#8217;t<br \/>\n    talked about lists yet.)<\/span>\n  <\/p>\n<p>\n    You can&#8217;t add commands, but you can create functions and macros. As a rule<br \/>\n    of thumb do not use a macro unless absolutely necessary, then you will avoid<br \/>\n    many problems.\n  <\/p>\n<h2 id=\"section-Scope\">Scope<\/h2>\n<p>\n    Scope is interesting in CMake and can occasionally be confusing. There&#8217;s<br \/>\n    local scope, directory scope, global scope, and cache scope. As with most<br \/>\n    languages things are inherited from enclosing scopes. For example if you<br \/>\n    were to set <code>someVariable<\/code> to &#8220;some value&#8221; and then call<br \/>\n    <code>someFunction()<\/code> inside the function dereferencing<br \/>\n    <code>someVariable<\/code> would yield &#8220;some value&#8221;.\n  <\/p>\n<h3 id=\"section-LocalScope\">Local Scope<\/h3>\n<p>\n    This refers to the most narrow scope at a given location. So the current<br \/>\n    function or directory if not inside a function. Note that conditionals,<br \/>\n    loops, and macros do not create a new scope, which is important to remember.<br \/>\n    When you set a variable this is the scope that is affected.\n  <\/p>\n<h3 id=\"section-ParentScope\">Parent Scope<\/h3>\n<p>\n    The scope enclosing the current local scope. For example the scope that<br \/>\n    called the current function or the directory that executed the most recent<br \/>\n    <code>add_subdirectory()<\/code> command. This is important because the<br \/>\n    <code>set()<\/code> command can be used to set variables in the parent<br \/>\n    scope. In fact this is the only way to return values from a function.\n  <\/p>\n<p class=\"code\">\n    set(<span class=\"arg\">variable<\/span><br \/>\n          <span class=\"arg\">values&hellip;<\/span><br \/>\n          PARENT_SCOPE)\n  <\/p>\n<p>\n    <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:set\"><code>set()<\/code> documentation<\/a>      <time class=\"access\" datetime=\"2013-06-01\">(2013-06-01)<\/time>  <\/p>\n<h3 id=\"section-DirectoryScope\">Directory Scope<\/h3>\n<p>\n    This is the scope of the current directory being processed by CMake which is<br \/>\n    used by directory properties and macros. The confusing thing is that some<br \/>\n    commands affect directory properties, such as<br \/>\n    <code><a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:add_definitions\">add_definitions()<\/a><\/code>    and<br \/>\n    <code><a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:remove_definitions\">remove_definitions()<\/a><\/code>.    Many of these properties affect the targets created within this directory<br \/>\n    scope but only take effect when generating. So if you create a target and<br \/>\n    then use the <code>add_definitions()<\/code> command those definitions will<br \/>\n    apply to the target created previously. It is less confusing if things that<br \/>\n    affect directory scope are done before creating any targets in that<br \/>\n    directory. Also do not mix setting directory properties and creating targets<br \/>\n    inside a function, either use separate functions or set the corresponding<br \/>\n    target property.\n  <\/p>\n<h3 id=\"section-GlobalScope\">Global Scope<\/h3>\n<p>\n    As expected anything defined with global scope is accessible from within any<br \/>\n    local scope. Targets, functions, and global properties all have global<br \/>\n    scope. For this reason all targets must have unique names.<br \/>\n    <span class=\"subtle\">(Strictly speaking this<br \/>\n    <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#prop_global:ALLOW_DUPLICATE_CUSTOM_TARGETS\">isn&#8217;t true<\/a>,    however not all generators can handle multiple targets with the same<br \/>\n    name. For maximum compatibility it is best to ensure all targets have unique<br \/>\n    names.)<\/span> Functions, on the other hand, can be redefined at will, but<br \/>\n    that is generally not a good idea.\n  <\/p>\n<h3 id=\"section-CacheScope\">Cache Scope<\/h3>\n<p>\n    This is similar to global scope, however only variables can be stored in the<br \/>\n    cache. In addition the cache persists between CMake configure runs. As we<br \/>\n    have already <a href=\"https:\/\/www.johnlamp.net\/cmake-tutorial-3-gui-tool.html#section-CmakeCache\">seen<\/a>    some cached variables can also be edited using the CMake GUI or the<br \/>\n    <code>ccmake<\/code> tool.\n  <\/p>\n<\/section>\n<section>\n<h1 id=\"section-Let'SIncludeSomeOrganization\">Let&#8217;s Include Some Organization<\/h1>\n<p>\n    There&#8217;s two issues with what we have now. First we&#8217;ve combined settings and<br \/>\n    functions for unit testing as well as an actual target. Second burying the<br \/>\n    inclusion of Google Mock this deep in our project makes it difficult to use<br \/>\n    a relative path. If you were to set the path to Google Mock on the command<br \/>\n    line using <kbd>cmake -DGMOCK_DIR=<span class=\"arg\">somePath<\/span><\/kbd><br \/>\n    you would expect the path to be relative to the top project directory rather<br \/>\n    than two directories deeper. We can fix both of these problems at the same<br \/>\n    time.\n  <\/p>\n<p>\n    We will refactor the code related to Google Mock into a separate file. Which<br \/>\n    will resolve problem one. Then we will include our new file from the top<br \/>\n    <code>CMakeLists.txt<\/code> file, which will address problem two. The<br \/>\n    question is where to put this new file and what to call it? In CMake files<br \/>\n    like these are called modules. Cmake comes with many which are stored in a<br \/>\n    directory called &#8220;Modules&#8221;. Many software projects, on the other hand, store<br \/>\n    CMake related code in a directory called &#8220;cmake&#8221;, a logical name, sometimes<br \/>\n    this is done out of necessity <span class=\"subtle\">(e.g. if using<br \/>\n    ClearCase)<\/span>. I think we shall put the file in<br \/>\n    <code>cmake\/Modules<\/code>. As for the name since we consistently used<br \/>\n    <code>gmock<\/code> or <code>GMOCK<\/code> let&#8217;s go with<br \/>\n    <code>gmock.cmake<\/code>.\n  <\/p>\n<section class=\"code cmake\">\n<header class=\"clear-after\">\n<h1>cmake\/Modules\/gmock.cmake<\/h1>\n<\/header>\n<div class=\"highlight\">\n<pre><a id=\"part2-cmake\/Modules\/gmock.cmake-1\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-1\"><\/a><span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">GMOCK_DIR<\/span> <span class=\"s2\">&quot;..\/..\/..\/gmock\/gmock-1.6.0&quot;<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-2\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-2\"><\/a>    <span class=\"s\">CACHE<\/span> <span class=\"s\">PATH<\/span> <span class=\"s2\">&quot;The path to the GoogleMock test framework.&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-3\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-3\"><\/a>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-4\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-4\"><\/a><span class=\"nb\">if<\/span> <span class=\"p\">(<\/span><span class=\"s2\">&quot;${CMAKE_CXX_COMPILER_ID}&quot;<\/span> <span class=\"s\">STREQUAL<\/span> <span class=\"s2\">&quot;MSVC&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-5\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-5\"><\/a>    <span class=\"c\"># force this option to ON so that Google Test will use \/MD instead of \/MT<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-6\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-6\"><\/a>    <span class=\"c\"># \/MD is now the default for Visual Studio, so it should be our default, too<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-7\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-7\"><\/a>    <span class=\"nb\">option<\/span><span class=\"p\">(<\/span><span class=\"s\">gtest_force_shared_crt<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-8\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-8\"><\/a>           <span class=\"s2\">&quot;Use shared (DLL) run-time lib even when Google Test is built as static lib.&quot;<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-9\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-9\"><\/a>           <span class=\"s\">ON<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-10\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-10\"><\/a><span class=\"nb\">elseif<\/span> <span class=\"p\">(<\/span><span class=\"s\">APPLE<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-11\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-11\"><\/a>    <span class=\"nb\">add_definitions<\/span><span class=\"p\">(<\/span><span class=\"s\">-DGTEST_USE_OWN_TR1_TUPLE=1<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-12\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-12\"><\/a><span class=\"nb\">endif<\/span><span class=\"p\">()<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-13\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-13\"><\/a><span class=\"nb\">add_subdirectory<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">CMAKE_BINARY_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/gmock<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-14\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-14\"><\/a><span class=\"nb\">set_property<\/span><span class=\"p\">(<\/span><span class=\"s\">TARGET<\/span> <span class=\"s\">gtest<\/span> <span class=\"s\">APPEND_STRING<\/span> <span class=\"s\">PROPERTY<\/span> <span class=\"s\">COMPILE_FLAGS<\/span> <span class=\"s2\">&quot; -w&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-15\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-15\"><\/a>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-16\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-16\"><\/a><span class=\"nb\">include_directories<\/span><span class=\"p\">(<\/span><span class=\"s\">SYSTEM<\/span> <span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/gtest\/include<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-17\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-17\"><\/a>                           <span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/include<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-18\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-18\"><\/a>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-19\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-19\"><\/a>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-20\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-20\"><\/a><span class=\"c\">#<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-21\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-21\"><\/a><span class=\"c\"># add_gmock_test(&lt;target&gt; &lt;sources&gt;...)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-22\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-22\"><\/a><span class=\"c\">#<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-23\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-23\"><\/a><span class=\"c\">#  Adds a Google Mock based test executable, &lt;target&gt;, built from &lt;sources&gt; and<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-24\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-24\"><\/a><span class=\"c\">#  adds the test so that CTest will run it. Both the executable and the test<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-25\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-25\"><\/a><span class=\"c\">#  will be named &lt;target&gt;.<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-26\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-26\"><\/a><span class=\"c\">#<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-27\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-27\"><\/a><span class=\"nb\">function<\/span><span class=\"p\">(<\/span><span class=\"s\">add_gmock_test<\/span> <span class=\"s\">target<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-28\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-28\"><\/a>    <span class=\"nb\">add_executable<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">ARGN<\/span><span class=\"o\">}<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-29\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-29\"><\/a>    <span class=\"nb\">target_link_libraries<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"s\">gmock_main<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-30\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-30\"><\/a>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-31\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-31\"><\/a>    <span class=\"nb\">add_test<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-32\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-32\"><\/a>\n<a id=\"part2-cmake\/Modules\/gmock.cmake-33\" class=\"line-number\" href=\"#part2-cmake\/Modules\/gmock.cmake-33\"><\/a><span class=\"nb\">endfunction<\/span><span class=\"p\">()<\/span>\n<\/pre>\n<\/div>\n<\/section>\n<p>\n    If you look closely the only change to this code you&#8217;ll notice is that the<br \/>\n    default value for <code>GMOCK_DIR<\/code> has two fewer parent directories in<br \/>\n    it. It is now relative to the top of our project as one would expect.\n  <\/p>\n<section class=\"code cmake\">\n<header class=\"clear-after\">\n<h1>CMakeLists.txt<\/h1>\n<div class=\"hll legend\">New or modified lines in bold.<\/div>\n<\/header>\n<div class=\"highlight\">\n<pre><a id=\"part2-CMakeLists.txt-1\" class=\"line-number\" href=\"#part2-CMakeLists.txt-1\"><\/a><span class=\"nb\">cmake_minimum_required<\/span><span class=\"p\">(<\/span><span class=\"s\">VERSION<\/span> <span class=\"s\">2.8<\/span> <span class=\"s\">FATAL_ERROR<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-2\" class=\"line-number\" href=\"#part2-CMakeLists.txt-2\"><\/a><span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">CMAKE_LEGACY_CYGWIN_WIN32<\/span> <span class=\"s\">0<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-3\" class=\"line-number\" href=\"#part2-CMakeLists.txt-3\"><\/a>\n<a id=\"part2-CMakeLists.txt-4\" class=\"line-number\" href=\"#part2-CMakeLists.txt-4\"><\/a><span class=\"nb\">project<\/span><span class=\"p\">(<\/span><span class=\"s2\">&quot;To Do List&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-5\" class=\"line-number\" href=\"#part2-CMakeLists.txt-5\"><\/a><span class=\"hll\">\n<\/span><a id=\"part2-CMakeLists.txt-6\" class=\"line-number\" href=\"#part2-CMakeLists.txt-6\"><\/a><span class=\"hll\"><span class=\"nb\">list<\/span><span class=\"p\">(<\/span><span class=\"s\">APPEND<\/span> <span class=\"s\">CMAKE_MODULE_PATH<\/span> <span class=\"o\">${<\/span><span class=\"nv\">CMAKE_SOURCE_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/cmake\/Modules<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"part2-CMakeLists.txt-7\" class=\"line-number\" href=\"#part2-CMakeLists.txt-7\"><\/a>\n<a id=\"part2-CMakeLists.txt-8\" class=\"line-number\" href=\"#part2-CMakeLists.txt-8\"><\/a><span class=\"nb\">enable_testing<\/span><span class=\"p\">()<\/span>\n<a id=\"part2-CMakeLists.txt-9\" class=\"line-number\" href=\"#part2-CMakeLists.txt-9\"><\/a><span class=\"hll\"><span class=\"nb\">include<\/span><span class=\"p\">(<\/span><span class=\"s\">gmock<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"part2-CMakeLists.txt-10\" class=\"line-number\" href=\"#part2-CMakeLists.txt-10\"><\/a>\n<a id=\"part2-CMakeLists.txt-11\" class=\"line-number\" href=\"#part2-CMakeLists.txt-11\"><\/a>\n<a id=\"part2-CMakeLists.txt-12\" class=\"line-number\" href=\"#part2-CMakeLists.txt-12\"><\/a><span class=\"nb\">if<\/span> <span class=\"p\">(<\/span><span class=\"s2\">&quot;${CMAKE_CXX_COMPILER_ID}&quot;<\/span> <span class=\"s\">STREQUAL<\/span> <span class=\"s2\">&quot;GNU&quot;<\/span> <span class=\"s\">OR<\/span>\n<a id=\"part2-CMakeLists.txt-13\" class=\"line-number\" href=\"#part2-CMakeLists.txt-13\"><\/a>    <span class=\"s2\">&quot;${CMAKE_CXX_COMPILER_ID}&quot;<\/span> <span class=\"s\">STREQUAL<\/span> <span class=\"s2\">&quot;Clang&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-14\" class=\"line-number\" href=\"#part2-CMakeLists.txt-14\"><\/a>    <span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">warnings<\/span> <span class=\"s2\">&quot;-Wall -Wextra -Werror&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-15\" class=\"line-number\" href=\"#part2-CMakeLists.txt-15\"><\/a><span class=\"nb\">elseif<\/span> <span class=\"p\">(<\/span><span class=\"s2\">&quot;${CMAKE_CXX_COMPILER_ID}&quot;<\/span> <span class=\"s\">STREQUAL<\/span> <span class=\"s2\">&quot;MSVC&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-16\" class=\"line-number\" href=\"#part2-CMakeLists.txt-16\"><\/a>    <span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">warnings<\/span> <span class=\"s2\">&quot;\/W4 \/WX \/EHsc&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-17\" class=\"line-number\" href=\"#part2-CMakeLists.txt-17\"><\/a><span class=\"nb\">endif<\/span><span class=\"p\">()<\/span>\n<a id=\"part2-CMakeLists.txt-18\" class=\"line-number\" href=\"#part2-CMakeLists.txt-18\"><\/a><span class=\"nb\">if<\/span> <span class=\"p\">(<\/span><span class=\"s\">NOT<\/span> <span class=\"s\">CONFIGURED_ONCE<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-19\" class=\"line-number\" href=\"#part2-CMakeLists.txt-19\"><\/a>    <span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">CMAKE_CXX_FLAGS<\/span> <span class=\"s2\">&quot;${warnings}&quot;<\/span>\n<a id=\"part2-CMakeLists.txt-20\" class=\"line-number\" href=\"#part2-CMakeLists.txt-20\"><\/a>        <span class=\"s\">CACHE<\/span> <span class=\"s\">STRING<\/span> <span class=\"s2\">&quot;Flags used by the compiler during all build types.&quot;<\/span> <span class=\"s\">FORCE<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-21\" class=\"line-number\" href=\"#part2-CMakeLists.txt-21\"><\/a>    <span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">CMAKE_C_FLAGS<\/span>   <span class=\"s2\">&quot;${warnings}&quot;<\/span>\n<a id=\"part2-CMakeLists.txt-22\" class=\"line-number\" href=\"#part2-CMakeLists.txt-22\"><\/a>        <span class=\"s\">CACHE<\/span> <span class=\"s\">STRING<\/span> <span class=\"s2\">&quot;Flags used by the compiler during all build types.&quot;<\/span> <span class=\"s\">FORCE<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-23\" class=\"line-number\" href=\"#part2-CMakeLists.txt-23\"><\/a><span class=\"nb\">endif<\/span><span class=\"p\">()<\/span>\n<a id=\"part2-CMakeLists.txt-24\" class=\"line-number\" href=\"#part2-CMakeLists.txt-24\"><\/a>\n<a id=\"part2-CMakeLists.txt-25\" class=\"line-number\" href=\"#part2-CMakeLists.txt-25\"><\/a>\n<a id=\"part2-CMakeLists.txt-26\" class=\"line-number\" href=\"#part2-CMakeLists.txt-26\"><\/a><span class=\"nb\">include_directories<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">CMAKE_CURRENT_SOURCE_DIR<\/span><span class=\"o\">}<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-27\" class=\"line-number\" href=\"#part2-CMakeLists.txt-27\"><\/a>\n<a id=\"part2-CMakeLists.txt-28\" class=\"line-number\" href=\"#part2-CMakeLists.txt-28\"><\/a><span class=\"nb\">add_subdirectory<\/span><span class=\"p\">(<\/span><span class=\"s\">ToDoCore<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-29\" class=\"line-number\" href=\"#part2-CMakeLists.txt-29\"><\/a>\n<a id=\"part2-CMakeLists.txt-30\" class=\"line-number\" href=\"#part2-CMakeLists.txt-30\"><\/a><span class=\"nb\">add_executable<\/span><span class=\"p\">(<\/span><span class=\"s\">toDo<\/span> <span class=\"s\">main.cc<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-31\" class=\"line-number\" href=\"#part2-CMakeLists.txt-31\"><\/a><span class=\"nb\">target_link_libraries<\/span><span class=\"p\">(<\/span><span class=\"s\">toDo<\/span> <span class=\"s\">toDoCore<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-CMakeLists.txt-32\" class=\"line-number\" href=\"#part2-CMakeLists.txt-32\"><\/a>\n<a id=\"part2-CMakeLists.txt-33\" class=\"line-number\" href=\"#part2-CMakeLists.txt-33\"><\/a>\n<a id=\"part2-CMakeLists.txt-34\" class=\"line-number\" href=\"#part2-CMakeLists.txt-34\"><\/a><span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">CONFIGURED_ONCE<\/span> <span class=\"s\">TRUE<\/span> <span class=\"s\">CACHE<\/span> <span class=\"s\">INTERNAL<\/span>\n<a id=\"part2-CMakeLists.txt-35\" class=\"line-number\" href=\"#part2-CMakeLists.txt-35\"><\/a>    <span class=\"s2\">&quot;A flag showing that CMake has configured at least once.&quot;<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/section>\n<dl>\n<dt class=\"code\">\n      list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}\/cmake\/Modules)\n    <\/dt>\n<dd>\n      Lists, finally! Okay not quite yet. Here we append the &#8220;Modules&#8221; directory<br \/>\n      we created to CMake&#8217;s module path. This is the path CMake searches when<br \/>\n      you include a module.\n    <\/dd>\n<dd>\n      We set the include path because, in the future, we might want to include<br \/>\n      modules from other <code>CMakeLists.txt<\/code> in other directories. This<br \/>\n      allows us to include them without having to specify the full path every<br \/>\n      time.\n    <\/dd>\n<dt class=\"code\">\n      include(gmock)\n    <\/dt>\n<dd>\n      This includes the new module we created. When used this way CMake searches<br \/>\n      the module path for the file <code>gmock.cmake<\/code> and when it finds<br \/>\n      the file it is included. These includes are much like those done by the C<br \/>\n      preprocessor. The code in the included file executes in the same scope as<br \/>\n      the file that included it.\n    <\/dd>\n<\/dl>\n<dl>\n<dt class=\"code\">\n      list(APPEND<br \/>\n           <span class=\"arg\">list<\/span><br \/>\n           <span class=\"arg\">elements&hellip;<\/span>)\n    <\/dt>\n<dd>\n      Appends the elements to the list stored in the variable named<br \/>\n      <code class=\"arg\">list<\/code>. That&#8217;s correct, you pass in the<br \/>\n      <em>name<\/em> of the list to be updated, you do not dereference it.\n    <\/dd>\n<dd>\n      <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:list\"><code>list()<\/code> documentation<\/a>      <time class=\"access\" datetime=\"2013-06-04\">(2013-06-04)<\/time>    <\/dd>\n<dt class=\"code\">\n      CMAKE_MODULE_PATH\n    <\/dt>\n<dd>\n      When including modules CMake searches for the requested module in the<br \/>\n      paths in this list. If this list is exhausted then CMake will look in the<br \/>\n      directory containing the default modules that come with CMake. Because<br \/>\n      these paths need to work anywhere in the build tree they must be absolute<br \/>\n      paths. Since this is a list the <code>list()<\/code> command should be used<br \/>\n      to manipulate it.\n    <\/dd>\n<dd>\n      <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#variable:CMAKE_MODULE_PATH\"><code>CMAKE_MODULE_PATH<\/code> documentation<\/a>      <time class=\"access\" datetime=\"2013-06-04\">(2013-06-04)<\/time>    <\/dd>\n<dt class=\"code\">\n      include(<span class=\"arg\">module<\/span> | <span class=\"arg\">file<\/span>)\n    <\/dt>\n<dd>\n      Include the module or file in the current file being processed. If a<br \/>\n      <span class=\"arg\">module<\/span> name is provided CMake will search for the<br \/>\n      file <code><span class=\"arg\">module<\/span>.cmake<\/code> and included it if<br \/>\n      found. Alternatively if a <code class=\"arg\">file<\/code> name is provided<br \/>\n      CMake will include that file directly; no module path searching is<br \/>\n      required. If the file cannot be included either because it doesn&#8217;t exist<br \/>\n      or wasn&#8217;t found CMake will issue a warning, but will continue processing.\n    <\/dd>\n<dd>\n      <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:include\"><code>include()<\/code> documentation<\/a>      <time class=\"access\" datetime=\"2013-06-04\">(2013-06-04)<\/time>    <\/dd>\n<\/dl>\n<section class=\"code cmake\">\n<header class=\"clear-after\">\n<h1>ToDoCore\/unit_test\/CMakeLists.txt<\/h1>\n<\/header>\n<div class=\"highlight\">\n<pre><a id=\"part2-ToDoCore\/unit_test\/CMakeLists.txt-1-1\" class=\"line-number\" href=\"#part2-ToDoCore\/unit_test\/CMakeLists.txt-1-1\"><\/a><span class=\"nb\">add_gmock_test<\/span><span class=\"p\">(<\/span><span class=\"s\">ToDoTest<\/span> <span class=\"s\">ToDoTest.cc<\/span><span class=\"p\">)<\/span>\n<a id=\"part2-ToDoCore\/unit_test\/CMakeLists.txt-1-2\" class=\"line-number\" href=\"#part2-ToDoCore\/unit_test\/CMakeLists.txt-1-2\"><\/a><span class=\"nb\">target_link_libraries<\/span><span class=\"p\">(<\/span><span class=\"s\">ToDoTest<\/span> <span class=\"s\">toDoCore<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/section>\n<p>\n    This file has gone on a serious diet. After moving all general code for unit<br \/>\n    testing with Google Mock into <code>gmock.cmake<\/code> this file became<br \/>\n    quite simple.\n  <\/p>\n<p>  <a class=\"sources\" href=\"https:\/\/www.johnlamp.net\/sources\/chapter5-2.zip\"><img src=\"https:\/\/www.johnlamp.net\/images\/zip.png\" alt=\"[zip file] \" \/>Source<\/a><\/section>\n<section>\n<h1 id=\"section-Lists!\">Lists!<\/h1>\n<p>\n    At long last! You&#8217;ve been teased by lists for 2 chapters now, and most of<br \/>\n    this one too. It is high time we discussed lists.\n  <\/p>\n<p>\n    CMake has two data structures built in: strings and lists. Well, strictly<br \/>\n    speaking that isn&#8217;t completely true; lists are semicolon delimited<br \/>\n    strings. So an empty string is also an empty list and a regular string is a<br \/>\n    list with only one item. The simplest way to make a list is<br \/>\n    <code>set(myList a b c)<\/code> which is exactly the same as<br \/>\n    <code>set(myList a;b;c)<\/code>. However <code>set(myList \"a;b;c\")<\/code><br \/>\n    creates a list with just one item. If a string begins with <code>\"<\/code><br \/>\n    it is treated as a string literal and any spaces or quotes remain a part of<br \/>\n    that string rather than causing it to be split into several list items.\n  <\/p>\n<p>\n    Lists are important to understand not just because they are useful but also<br \/>\n    because all arguments to commands, functions, and macros are processed as a<br \/>\n    list. So just as <code>set(myList a b c)<\/code> is the same as<br \/>\n    <code>set(myList a;b;c)<\/code> so too is<br \/>\n    <code>set(myList;a;b;c)<\/code>. When CMake processes the call to the<br \/>\n    <code>set()<\/code> command it collects all of the arguments into a single<br \/>\n    list. This list <span class=\"subtle\">(<code>ARGV<\/code>)<\/span> is the<br \/>\n    separated into the first argument, the variable name<br \/>\n    <span class=\"subtle\">(<code>myList<\/code>)<\/span>, and the rest of the<br \/>\n    items, the values <span class=\"subtle\">(<code>a;b;c<\/code>)<\/span>. This can<br \/>\n    cause trouble if you pass a quoted string containing semicolons to a<br \/>\n    function that then passes it to another function without quoting it as your<br \/>\n    string will become a list.\n  <\/p>\n<p>\n    While you can create list with <code>set(myList a b c)<\/code> I&#8217;d strongly<br \/>\n    recommend using <code>list(APPEND myList a b c)<\/code>. Using the<br \/>\n    <code>list()<\/code> command shows that you are using the variable<br \/>\n    <code>myList<\/code> as a list. Naturally the <code>list()<\/code> command<br \/>\n    allows you to do other things with lists.\n  <\/p>\n<p>\n    <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:list\"><code>list()<\/code> documentation<\/a>    <time class=\"access\" datetime=\"2013-06-04\">(2013-06-04)<\/time>  <\/p>\n<\/section>\n<section>\n<h1 id=\"section-AutoPlay\">Auto Play<\/h1>\n<p>\n    Well really automatic test running. So far in my experience it takes<br \/>\n    significantly less time to run unit tests than it does to build them. For<br \/>\n    this reason I think it is beneficial to run your unit tests every time they<br \/>\n    are built. This also has the side effect of stopping your build if the unit<br \/>\n    test fails.\n  <\/p>\n<section class=\"code cmake\">\n<header class=\"clear-after\">\n<h1>cmake\/Modules\/gmock.cmake<\/h1>\n<div class=\"hll legend\">New or modified lines in bold.<\/div>\n<\/header>\n<div class=\"highlight\">\n<pre><a id=\"cmake\/Modules\/gmock.cmake-1-1\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-1\"><\/a><span class=\"nb\">set<\/span><span class=\"p\">(<\/span><span class=\"s\">GMOCK_DIR<\/span> <span class=\"s2\">&quot;..\/..\/..\/gmock\/gmock-1.6.0&quot;<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-2\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-2\"><\/a>    <span class=\"s\">CACHE<\/span> <span class=\"s\">PATH<\/span> <span class=\"s2\">&quot;The path to the GoogleMock test framework.&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-3\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-3\"><\/a>\n<a id=\"cmake\/Modules\/gmock.cmake-1-4\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-4\"><\/a><span class=\"nb\">if<\/span> <span class=\"p\">(<\/span><span class=\"s2\">&quot;${CMAKE_CXX_COMPILER_ID}&quot;<\/span> <span class=\"s\">STREQUAL<\/span> <span class=\"s2\">&quot;MSVC&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-5\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-5\"><\/a>    <span class=\"c\"># force this option to ON so that Google Test will use \/MD instead of \/MT<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-6\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-6\"><\/a>    <span class=\"c\"># \/MD is now the default for Visual Studio, so it should be our default, too<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-7\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-7\"><\/a>    <span class=\"nb\">option<\/span><span class=\"p\">(<\/span><span class=\"s\">gtest_force_shared_crt<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-8\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-8\"><\/a>           <span class=\"s2\">&quot;Use shared (DLL) run-time lib even when Google Test is built as static lib.&quot;<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-9\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-9\"><\/a>           <span class=\"s\">ON<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-10\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-10\"><\/a><span class=\"nb\">elseif<\/span> <span class=\"p\">(<\/span><span class=\"s\">APPLE<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-11\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-11\"><\/a>    <span class=\"nb\">add_definitions<\/span><span class=\"p\">(<\/span><span class=\"s\">-DGTEST_USE_OWN_TR1_TUPLE=1<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-12\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-12\"><\/a><span class=\"nb\">endif<\/span><span class=\"p\">()<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-13\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-13\"><\/a><span class=\"nb\">add_subdirectory<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">CMAKE_BINARY_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/gmock<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-14\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-14\"><\/a><span class=\"nb\">set_property<\/span><span class=\"p\">(<\/span><span class=\"s\">TARGET<\/span> <span class=\"s\">gtest<\/span> <span class=\"s\">APPEND_STRING<\/span> <span class=\"s\">PROPERTY<\/span> <span class=\"s\">COMPILE_FLAGS<\/span> <span class=\"s2\">&quot; -w&quot;<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-15\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-15\"><\/a>\n<a id=\"cmake\/Modules\/gmock.cmake-1-16\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-16\"><\/a><span class=\"nb\">include_directories<\/span><span class=\"p\">(<\/span><span class=\"s\">SYSTEM<\/span> <span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/gtest\/include<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-17\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-17\"><\/a>                           <span class=\"o\">${<\/span><span class=\"nv\">GMOCK_DIR<\/span><span class=\"o\">}<\/span><span class=\"s\">\/include<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-18\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-18\"><\/a>\n<a id=\"cmake\/Modules\/gmock.cmake-1-19\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-19\"><\/a>\n<a id=\"cmake\/Modules\/gmock.cmake-1-20\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-20\"><\/a><span class=\"c\">#<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-21\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-21\"><\/a><span class=\"c\"># add_gmock_test(&lt;target&gt; &lt;sources&gt;...)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-22\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-22\"><\/a><span class=\"c\">#<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-23\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-23\"><\/a><span class=\"c\">#  Adds a Google Mock based test executable, &lt;target&gt;, built from &lt;sources&gt; and<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-24\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-24\"><\/a><span class=\"c\">#  adds the test so that CTest will run it. Both the executable and the test<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-25\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-25\"><\/a><span class=\"c\">#  will be named &lt;target&gt;.<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-26\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-26\"><\/a><span class=\"c\">#<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-27\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-27\"><\/a><span class=\"nb\">function<\/span><span class=\"p\">(<\/span><span class=\"s\">add_gmock_test<\/span> <span class=\"s\">target<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-28\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-28\"><\/a>    <span class=\"nb\">add_executable<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">ARGN<\/span><span class=\"o\">}<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-29\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-29\"><\/a>    <span class=\"nb\">target_link_libraries<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"s\">gmock_main<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-30\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-30\"><\/a>\n<a id=\"cmake\/Modules\/gmock.cmake-1-31\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-31\"><\/a>    <span class=\"nb\">add_test<\/span><span class=\"p\">(<\/span><span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span> <span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span><span class=\"p\">)<\/span>\n<a id=\"cmake\/Modules\/gmock.cmake-1-32\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-32\"><\/a>\n<a id=\"cmake\/Modules\/gmock.cmake-1-33\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-33\"><\/a><span class=\"hll\">    <span class=\"nb\">add_custom_command<\/span><span class=\"p\">(<\/span><span class=\"s\">TARGET<\/span> <span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span>\n<\/span><a id=\"cmake\/Modules\/gmock.cmake-1-34\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-34\"><\/a><span class=\"hll\">                       <span class=\"s\">POST_BUILD<\/span>\n<\/span><a id=\"cmake\/Modules\/gmock.cmake-1-35\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-35\"><\/a><span class=\"hll\">                       <span class=\"s\">COMMAND<\/span> <span class=\"o\">${<\/span><span class=\"nv\">target<\/span><span class=\"o\">}<\/span>\n<\/span><a id=\"cmake\/Modules\/gmock.cmake-1-36\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-36\"><\/a><span class=\"hll\">                       <span class=\"s\">WORKING_DIRECTORY<\/span> <span class=\"o\">${<\/span><span class=\"nv\">CMAKE_CURRENT_BINARY_DIR<\/span><span class=\"o\">}<\/span>\n<\/span><a id=\"cmake\/Modules\/gmock.cmake-1-37\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-37\"><\/a><span class=\"hll\">                       <span class=\"s\">COMMENT<\/span> <span class=\"s2\">&quot;Running ${target}&quot;<\/span> <span class=\"s\">VERBATIM<\/span><span class=\"p\">)<\/span>\n<\/span><a id=\"cmake\/Modules\/gmock.cmake-1-38\" class=\"line-number\" href=\"#cmake\/Modules\/gmock.cmake-1-38\"><\/a><span class=\"nb\">endfunction<\/span><span class=\"p\">()<\/span>\n<\/pre>\n<\/div>\n<\/section>\n<p>  <a class=\"sources\" href=\"https:\/\/www.johnlamp.net\/sources\/chapter5-3.zip\"><img src=\"https:\/\/www.johnlamp.net\/images\/zip.png\" alt=\"[zip file] \" \/>Source<\/a><\/p>\n<dl>\n<dt class=\"code\">\n<pre>\nadd_custom_command(TARGET ${target}\n                   POST_BUILD\n                   COMMAND .\/${target}\n                   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}\n                   COMMENT \"Running ${target}\" VERBATIM)<\/pre>\n<\/dt>\n<dd>\n      We use the <code>add_custom_command()<\/code> command to run each test<br \/>\n      after each time it is built. Here we simply run the test and if it fails<br \/>\n      the build will stop. However if you were to build again immediately the<br \/>\n      failed test would <b>not<\/b> be run again and the build will<br \/>\n      continue. Fixing that will be left for later.\n    <\/dd>\n<\/dl>\n<dl>\n<dt class=\"code\">\n<pre>\nadd_custom_command(TARGET <span class=\"arg\">target<\/span>\n                   PRE_BUILD | PRE_LINK | POST_BUILD\n                   COMMAND <span class=\"arg\">command<\/span> <span class=\"optional arg\">arguments&hellip;<\/span>\n                   <span class=\"optional\">COMMAND <span class=\"arg\">command2<\/span> <span class=\"optional arg\">arguments&hellip;<\/span> &hellip;<\/span>\n                   <span class=\"optional\">WORKING_DIRECTORY <span class=\"arg\">directory<\/span><\/span>\n                   <span class=\"optional\">COMMENT <span class=\"arg\">comment<\/span><\/span> <span class=\"optional\">VERBATIM<\/span>)<\/pre>\n<\/dt>\n<dd>\n<dl>\n<dt class=\"code arg\">\n          target\n        <\/dt>\n<dd>\n          The name of the target to which we are adding the custom command.\n        <\/dd>\n<dt class=\"code\">\n          PRE_BUILD | PRE_LINK | POST_BUILD\n        <\/dt>\n<dd>\n          When to run the custom command. <code>PRE_BUILD<\/code> will run the<br \/>\n          command before any of the target&#8217;s other<br \/>\n          dependencies. <code>PRE_LINK<\/code> runs the command after all other<br \/>\n          dependencies. Lastly <code>POST_BUILD<\/code> runs the command after<br \/>\n          the target has been built.\n        <\/dd>\n<dd class=\"note\">\n          <em>Note:<\/em> the <code>PRE_BUILD<\/code> option only works with Visual<br \/>\n          Studio 7 or newer. For all other generators it is treated as<br \/>\n          <code>PRE_LINK<\/code> instead.\n        <\/dd>\n<dt class=\"code\">\n          COMMAND <span class=\"arg\">command<\/span> <span class=\"optional arg\">arguments&hellip;<\/span>\n        <\/dt>\n<dd>\n          The command to run and any arguments to be passed to it. If<br \/>\n          <code class=\"arg\">command<\/code> specifies an executable target,<br \/>\n          i.e. one created with the <code>add_executable()<\/code> command, the<br \/>\n          location of the actual built executable will replace the name;<br \/>\n          additionally a target level dependency will be added so that the<br \/>\n          executable target will be built before this custom command is run.\n        <\/dd>\n<dd class=\"note\">\n          <em>Note:<\/em> target level dependencies merely control the order in<br \/>\n          which targets are build. If a target level dependency is rebuilt this<br \/>\n          command will not be re-run.\n        <\/dd>\n<dd>\n          Any number of commands can be listed using this syntax and they will<br \/>\n          all be run in order each time.\n        <\/dd>\n<dt class=\"code optional\">\n          WORKING_DIRECTORY <span class=\"arg\">directory<\/span>\n        <\/dt>\n<dd>\n          Specify the working directory from which the listed commands will be<br \/>\n          run.\n        <\/dd>\n<dt class=\"code optional\">\n          COMMENT <span class=\"arg\">comment<\/span>\n        <\/dt>\n<dd>\n          Provide a comment that will be displayed before the listed commands<br \/>\n          are run.\n        <\/dd>\n<dt class=\"code optional\">\n          VERBATIM\n        <\/dt>\n<dd>\n          This argument tells CMake to ensure that the commands and their<br \/>\n          arguments are escaped appropriately for whichever build tool is being<br \/>\n          used. If this argument is omitted the behavior is platform and tool<br \/>\n          specific. Therefore it is <strong>strongly<\/strong> recommended that you<br \/>\n          always provide the <code>VERBATIM<\/code> argument.\n        <\/dd>\n<\/dl>\n<\/dd>\n<dd>\n      <a href=\"http:\/\/www.cmake.org\/cmake\/help\/v2.8.10\/cmake.html#command:add_custom_command\"><code>add_custom_command()<\/code> documentation<\/a>      <time class=\"access\" datetime=\"2013-06-15\">(2013-06-15)<\/time>    <\/dd>\n<\/dl>\n<p>\n    Now it&#8217;s time to see our hard work in action.\n  <\/p>\n<section class=\"terminal\">\n<pre>\n <span class=\"ansiBold ansiForeground-blue\">&gt;<\/span> mkdir build\n <span class=\"ansiBold ansiForeground-blue\">&gt;<\/span> cd build\n <span class=\"ansiBold ansiForeground-blue\">&gt;<\/span> cmake -G &quot;Unix Makefiles&quot; ..\n-- The C compiler identification is Clang 4.2.0\n-- The CXX compiler identification is Clang 4.2.0\n-- Check for working C compiler: \/usr\/bin\/cc\n-- Check for working C compiler: \/usr\/bin\/cc -- works\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Check for working CXX compiler: \/usr\/bin\/c++\n-- Check for working CXX compiler: \/usr\/bin\/c++ -- works\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Found PythonInterp: \/usr\/local\/bin\/python (found version &quot;2.7.3&quot;)\n-- Looking for include file pthread.h\n-- Looking for include file pthread.h - found\n-- Looking for pthread_create\n-- Looking for pthread_create - found\n-- Found Threads: TRUE\n-- Configuring done\n-- Generating done\n-- Build files have been written to: \/Documents\/Programming\/CMake\/CMake Tutorial\/flavors\/part5_step3\/build\n <span class=\"ansiBold ansiForeground-blue\">&gt;<\/span> make\n<span class=\"ansiBold ansiForeground-magenta\">Scanning dependencies of target toDoCore\n<\/span>[ 14%] <span class=\"ansiForeground-green\">Building CXX object ToDoCore\/CMakeFiles\/toDoCore.dir\/ToDo.cc.o\n<\/span><span class=\"ansiBold ansiForeground-red\">Linking CXX static library libtoDoCore.a\n<\/span>[ 14%] Built target toDoCore\n<span class=\"ansiBold ansiForeground-magenta\">Scanning dependencies of target toDo\n<\/span>[ 28%] <span class=\"ansiForeground-green\">Building CXX object CMakeFiles\/toDo.dir\/main.cc.o\n<\/span><span class=\"ansiBold ansiForeground-red\">Linking CXX executable toDo\n<\/span>[ 28%] Built target toDo\n<span class=\"ansiBold ansiForeground-magenta\">Scanning dependencies of target gtest\n<\/span>[ 42%] <span class=\"ansiForeground-green\">Building CXX object gmock\/gtest\/CMakeFiles\/gtest.dir\/src\/gtest-all.cc.o\n<\/span><span class=\"ansiBold ansiForeground-red\">Linking CXX static library libgtest.a\n<\/span>[ 42%] Built target gtest\n<span class=\"ansiBold ansiForeground-magenta\">Scanning dependencies of target gmock\n<\/span>[ 57%] <span class=\"ansiForeground-green\">Building CXX object gmock\/CMakeFiles\/gmock.dir\/src\/gmock-all.cc.o\n<\/span><span class=\"ansiBold ansiForeground-red\">Linking CXX static library libgmock.a\n<\/span>[ 57%] Built target gmock\n<span class=\"ansiBold ansiForeground-magenta\">Scanning dependencies of target gmock_main\n<\/span>[ 71%] <span class=\"ansiForeground-green\">Building CXX object gmock\/CMakeFiles\/gmock_main.dir\/src\/gmock_main.cc.o\n<\/span><span class=\"ansiBold ansiForeground-red\">Linking CXX static library libgmock_main.a\n<\/span>[ 71%] Built target gmock_main\n<span class=\"ansiBold ansiForeground-magenta\">Scanning dependencies of target gtest_main\n<\/span>[ 85%] <span class=\"ansiForeground-green\">Building CXX object gmock\/gtest\/CMakeFiles\/gtest_main.dir\/src\/gtest_main.cc.o\n<\/span><span class=\"ansiBold ansiForeground-red\">Linking CXX static library libgtest_main.a\n<\/span>[ 85%] Built target gtest_main\n<span class=\"ansiBold ansiForeground-magenta\">Scanning dependencies of target ToDoTest\n<\/span>[100%] <span class=\"ansiForeground-green\">Building CXX object ToDoCore\/unit_test\/CMakeFiles\/ToDoTest.dir\/ToDoTest.cc.o\n<\/span><span class=\"ansiBold ansiForeground-red\">Linking CXX executable ToDoTest\n<\/span><span class=\"ansiBold ansiForeground-blue\">Running ToDoTest\n<\/span>Running main() from gmock_main.cc\n<span class=\"ansiForeground-green\">[==========] <\/span>Running 4 tests from 1 test case.\n<span class=\"ansiForeground-green\">[----------] <\/span>Global test environment set-up.\n<span class=\"ansiForeground-green\">[----------] <\/span>4 tests from ToDoTest\n<span class=\"ansiForeground-green\">[ RUN      ] <\/span>ToDoTest.constructor_createsEmptyList\n<span class=\"ansiForeground-green\">[       OK ] <\/span>ToDoTest.constructor_createsEmptyList (0 ms)\n<span class=\"ansiForeground-green\">[ RUN      ] <\/span>ToDoTest.addTask_threeTimes_sizeIsThree\n<span class=\"ansiForeground-green\">[       OK ] <\/span>ToDoTest.addTask_threeTimes_sizeIsThree (0 ms)\n<span class=\"ansiForeground-green\">[ RUN      ] <\/span>ToDoTest.getTask_withOneTask_returnsCorrectString\n<span class=\"ansiForeground-green\">[       OK ] <\/span>ToDoTest.getTask_withOneTask_returnsCorrectString (0 ms)\n<span class=\"ansiForeground-green\">[ RUN      ] <\/span>ToDoTest.getTask_withThreeTasts_returnsCorrectStringForEachIndex\n<span class=\"ansiForeground-green\">[       OK ] <\/span>ToDoTest.getTask_withThreeTasts_returnsCorrectStringForEachIndex (0 ms)\n<span class=\"ansiForeground-green\">[----------] <\/span>4 tests from ToDoTest (0 ms total)\n<span class=\"ansiForeground-green\">[----------] <\/span>Global test environment tear-down\n<span class=\"ansiForeground-green\">[==========] <\/span>4 tests from 1 test case ran. (0 ms total)\n<span class=\"ansiForeground-green\">[  PASSED  ] <\/span>4 tests.\n[100%] Built target ToDoTest\n<\/pre>\n<\/section>\n<p>\n    It still works, just it&#8217;s more automatic now.\n  <\/p>\n<\/section>\n<section id=\"revision-history\"><span class=\"heading\">Revision History<\/span><\/p>\n<table summary=\"Revision History\">\n<thead>\n<tr>\n<td>Version<\/td>\n<td>Date<\/td>\n<td>Comment<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2013-07-14<\/td>\n<td>Original version.<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2014-10-01<\/td>\n<td>Added the work around for a problem with Google Test and newer versions of Mac OS X introduced in Chapter 4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Last time we added a nice unit test and then set up CMake to build it, of<br \/>\n    course, and add it to the list of tests that CTest will run. This is great,<br \/>\n    now we can run cmake then use make and make<br \/>\n    test to test our project. Now it&#8217;s time to build on our &hellip;<br \/><a href=\"https:\/\/www.johnlamp.net\/?p=29\" rel=\"bookmark\">Continue reading &quot;CMake Tutorial &#8211; Chapter&#160;5: Functionally Improved Testing&quot;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[3,4,5],"_links":{"self":[{"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=\/wp\/v2\/posts\/29"}],"collection":[{"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":0,"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johnlamp.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}