Let’s take the following example:
add_library(tw_base INTERFACE)
target_sources(tw_base INTERFACE Data.cpp)
target_include_directories(tw_base INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(tw_base PROPERTIES PUBLIC_HEADER Data.h)
add_library(tw::base ALIAS tw_base)
if (TW_UNIT_TESTS)
add_library(tw_base_tests INTERFACE)
target_sources(tw_base_tests INTERFACE tests/DataTests.cpp)
target_link_libraries(tw_base_tests INTERFACE tw::base)
add_library(tw::base_tests ALIAS tw_base_tests)
endif()
add_library(MyAwesomeStaticLib sources...)
target_link_libraries(MyAwesomeStaticLib PUBLIC tw::base)
Later in tests folder:
add_executable(tests ${test_sources})
target_link_libraries(tests gtest_main MyAwesomeStaticLib tw::base_tests)
Why Data.cpp is included in multiple unity files (tests sources and lib sources) ? I would like Data.cpp to be compiled in unity build only for MyAwesomeStaticLib
I would like Data.cpp to be compiled only on MyAwesomeStaticLib and not the unit test target, Any hints on how to do that properly?