1

I want a cmake project with two configurations BUILD and TEST.

BUILD compiles all sources not in test subdirectory into a shared library. TEST compiles all sources, including those in test subdirectory (which includes main.cpp) into an executable that runs the tests. I don't want TEST to build the shared library. I don't want BUILD to build the test executable.

I currently have it on disk as:

project/ test/ test_foo.cpp main.cpp bar.hpp widget.hpp bar.cpp widget.cpp ... 

I can move things around if it makes it easier. What do I put in my CMakeLists.txt files?

3
  • @JoachimPileborg He's asking how his CMakeLists.txt would look for the configuration he wants, how is that opinion-based? Commented Aug 1, 2015 at 20:38
  • You might want to edit your question to make it clearer what you want, and especially change the title. Commented Aug 1, 2015 at 20:40
  • I think Joachim means something like his question aims to much on "do my work". "What do I put in..." is a bad way to ask. Commented Aug 1, 2015 at 21:10

1 Answer 1

5

It looks to me like you want to use cmake's OPTION command. Pick one configuration to be on by default (or neither if you want to force the person compiling the code to choose)

OPTION( BUILD_SHARED_LIBRARY "Compile sources into shared library" ON ) OPTION( RUN_TESTS "Compile test executable and run it" OFF ) 

You'll want to make sure that the options are mutually exclusive, and error out otherwise

if ( BUILD_SHARED_LIBRARY AND RUN_TESTS ) message(FATAL_ERROR "Can't build shared library and run tests at same time") endif() 

You can then put the rest of your commands inside if blocks based on those variables

if ( BUILD_SHARED_LIBRARY ) #add subdirectories except for test, add sources to static library, etc endif() if ( RUN_TESTS ) #compile an executable and run it, etc. endif() 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I managed to get things working based on this example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.