Using CLion, I have a project structure that looks like this:
You'll see that in the root there are an include
directory, a src
directory, a lib
directory, a cmake-build-debug
directory, a CMakeLists.txt
file (see red arrow), and a tests
directory. The project builds a shared object into lib
using the root-level CMakeLists.txt
file and the various source, header, and external files.
I can successfully run the root-level CMakeList.txt
file and build the library. So far, so good.
Here is the issue. In addition to the above, I am now interested -- on an informal, exploratory basis -- in testing some of the code in the project. To that end, as an initial example, I have created a test_nnls
directory with its own CMakeLists.txt
(see yellow arrow) file and a test_nnls.cpp
file. The code in test_nnls.cpp
creates some dummy data, calls a function in src/nnls.cpp
, and prints the results. The build process here just creates an executable that does this. This approach is not meant to be part of any formal test framework and I do not want the test to run as part of the root-level build. This is just me adding a simple test program in the overall project that I would like to compile and run on an independent basis when I feel the need. I plan to possibly implement a formal test framework later, but at present I don't know how to do this and for now just need to print some simple output to see if the chosen code is working correctly.
The problem is that I cannot seem to execute the CMakeLists.txt
file (yellow arrow) to build the test. It does not appear to be possible in CLion to set up a build for the test program using cmake
. How do I structure all of this to get what I want? Thanks.
You don't execute a CMakeLists.txt
. It is read by cmake
(the root CMaksLists.txt
file), which is called by CLion. However, CLion only passes the root CMaksLists.txt
file to cmake
. Even if you call cmake yourself you would only pass this root CMaksLists.txt
file.
If you want to define targets (or anything) in other CMaksLists.txt
files located in other folders, then you must add add_subdirectory(folder_name_that_contains_another_CMakeLists_file)
to your root CMaksLists.txt
file. Only then targets in these other CMaksLists.txt
files will appear in CLion.
Note that a few things should appear in the root CMaksLists.txt
file, but not in the other ones. Particularly, the two lines below should only be in the root file
cmake_minimum_required(VERSION 3.14) # Choose the minimum cmake version
project(name_of_your_project)