3

Using CLion, I have a project structure that looks like this:

enter image description here

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.

1
  • 1
    Include the tests from your root CMakeLists.txt, then exclude your test programs from the main build either by adding EXCLUDE_FROM_ALL to the add_subdirectory call OR hiding everything behind an if (TESTING) block. Commented Oct 28, 2020 at 18:38

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Once I have added the two lines you suggested in the root-level CMakeLists.txt file and CLion has created a separate target entry for the target in the lower level CMakeLists.txt, does this mean that I can somehow tell CLion/cmake to follow only the instructions in the lower level file? I didn't want to run the root-level instructions.
I am beginning to wonder whether, given my interest in separating the running of instructions in the root-level file from those in the lower file, I would be better off moving the test code and corresponding CMakeLists.txt file to another project/directory.
Braking the configuration into multiple CMakeLists.txt files is more a matter of organization. Everything is a single project and that is why only the root CMakeLists.txt file should have these lines. What is the benefit of having the test code in a different project? You can choose exactly which target you want to compile and run n CLion. If you care about the target all and you would not like the tests target to be include in it the target all, then just add EXCLUDE_FROM_ALL to the tests target as in @Botje's comment.
Actually, after some more fiddling around, I am now able to make everything work as hoped. I've kept the tests in the same project. Thanks.
If you think this answer fully answers your question then you can accept it. Unless there is something more. In that case you can comment here such that either me or something else can answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.