30

I have a project in which i have essentially two main methods. One for testing and one for, well, running the code. Normally you would create submodules, but this is not an option.

file(GLOB sources "*.cpp") file(GLOB headers "*.h") add_executable(testing ${sources} ${headers}) add_executable(main ${sources} ${headers}) 

So testing should compile all sources except for main.cpp. Main should compile everything but testing.cpp.

1

1 Answer 1

61

The normal way would probably be to create a library from all the sources except main.cpp and testing.cpp, then link this to each executable. However, I guess you mean you can't do that when you say you can't create submodules.

Instead, you can use the list(REMOVE_ITEM ...) command:

file(GLOB sources "*.cpp") file(GLOB headers "*.h") set(testing_sources ${sources}) list(REMOVE_ITEM testing_sources ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) list(REMOVE_ITEM sources ${CMAKE_CURRENT_SOURCE_DIR}/testing.cpp) add_executable(main ${sources} ${headers}) add_executable(testing ${testing_sources} ${headers}) 
Sign up to request clarification or add additional context in comments.

3 Comments

Does this work? 'Cause I get list sub-command REMOVE_ITEM requires list to be present. error under cmake 3.5.0, windows 10 with the code above.
Yes, this works under CMake 3.5.0. Probably given your error message, you've either "dereferenced" your list (e.g. done list(REMOVE_ITEM ${sources} ...) rather than list(REMOVE_ITEM sources ...)), or your list is empty.
Not knowing, I tried to not use ${CMAKE_CURRENT_SOURCE_DIR} in front of the filename to be removed, but it doesn't work. Prepending the filename(s) with ${CMAKE_CURRENT_SOURCE_DIR} is mandatory. My 2 cents. Ste

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.