
In the next installment of this series, I’ll go into a few more useful properties. With just a handful of commands we can already maintain a clean CMake project structure. And, as always, you can see the working project in this state on GitHub. No surprises here: we have the tests executable that links against the catch and hello_lib libraries. Target_link_libraries(tests PRIVATE hello_lib catch) The last CMakeLists.txt to examine is the one in the test directory: project(hello_tests) The main program simply has the main.cpp and links against the library containing the rest of the code, as we have seen before. The library target again has the INCLUDE_DIRECTORIES property set, so the tests won’t have to add the directory manually.
#Cmake print code#
This is a good thing, because not only does it cost less time we also want to make sure that the code under test is compiled with the same flags properties as in production. The scope keyword and its arguments must come before the PROPERTIES keyword, in the arguments list. Exactly one of the scope keywords must be used. In addition, hello.cpp is only compiled once, and not for both the tests and the main program. This function prints the values of the properties of the given targets, source files, directories, tests or cache entries. The result will be a -I flag for the compiler on GCC or Clang, a /I flag for the MSVC compiler, and so on.Īdd_executable(tests testmain.cpp $ variable we had before. That is why we have to use the target_include_directories command to add an INCLUDE_DIRECTORY property to our tests target. Of course, the tests won’t build now, since catch.hpp is not where it used to be and the compiler won’t find it without a proper include path.

The structure now looks like this: hello_cmake We’ll put other third party stuff that may come under thirdparty as well, each in their own directory. This will be written to CMakeLists.txt and a few initial source files. Let’s move the catch.hpp into its own directory thirdparty/catch/include. Open the Command Palette ( Ctrl+Shift+P) and run the CMake: Quick Start command: Enter a project name. I’ve written about structuring our code into directories, and today we’ll move our project in that direction. Sources for the main program, sources for the tests, the Catch header, the CMakeLists.txt – everything. Can somebody please enlighten me as to what the command line flag to CMake might be that will make it.
#Cmake print series#
This post is the third of a series about CMake:Ĭurrently, the files of our little CMake project are all in the same directory. As promised in the last post about CMake, today we’ll use a proper CMake project structure for our “Hello CMake” project.
