Avoid recompiling shared object files with CMake?

I recently added testing through CMake to one of my projects. I did this by creating another executable that will run my test cases. The test examples in my project use code from my main application. Each time I make changes to the source file, which is shared by both the main application and the test runner, it recompiles this object twice. Once for the main application and second time for the test runner.

Is there a way that I can use the same object files for both?

My CMakeLists file looks something like this.

AUX_SOURCE_DIRECTORY(${SRC_DIR}/game game_SRC) AUX_SOURCE_DIRECTORY(${SRC_DIR}/framework/ framework_SRC) ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} ${game_SRC} ${framework_SRC}) # --- Testing --- ENABLE_TESTING() AUX_SOURCE_DIRECTORY(${TEST_DIR} test_SRC) ADD_EXECUTABLE(${TEST_RUNNER_NAME} ${test_SRC} ${framework_SRC} ) 
+4
source share
1 answer

Yes, making your framework a separate library. As of now, you specify framework_SRCS as sources for the project executable, and then specify the same sources for the test-runner executable. And CMake simply creates both executable files from the specified sources.

Worse, CMake cannot easily assume that the same source file will be used the same for both executable files. What if you have several different compilation flags between your test and your application?

The easiest way is to link you to framework_SRCS in the library, and then specify the link dependency:

 ADD_LIBRARY( MyFramework STATIC ${framework_SRCS} ) ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} ${game_SRC}) TARGET_LINK_LIBRARIES( ${CMAKE_PROJECT_NAME} MyFramework ) # --- Testing --- ENABLE_TESTING() AUX_SOURCE_DIRECTORY(${TEST_DIR} test_SRC) ADD_EXECUTABLE(${TEST_RUNNER_NAME} ${test_SRC} TARGET_LINK_LIBRARIES( ${TEST_RUNNER_NAME} MyFramework ) 

(Note that I explicitly decided to build the library statically. Of course, you could leave it until cmake or force the shared assembly to be used)

Regards, Andre

+13
source

Source: https://habr.com/ru/post/1334486/


All Articles