Hi to All, I’m new to cmake and I’m struggling a bit creating a configuration for my case:
I’m working on a desktop application in c++ that depend on various external libraries. In that project I have 3 cmake targets: one for the main executable, one for the unit testing and one for the benchmarks.
The main sources of the project, most of the external libraries and some compile definitions are shared between the targets. Some other sources, libraries and compile definitions are present only in one of the target.
Some of the compile definitions change the program execution logic through #ifdef macro to make the code unit testable.
I was trying to structure the project in this way:
A main OBJECT library that hold the common sources between the targets, defined as:
add_library (EngineSources OBJECT <<<COMMON_SOURCES>>>)
An INTERFACE library that hold the external libraries:
add_library (EngineDependencies INTERFACE)
target_compile_definitions (EngineDependencies
INTERFACE
<<<COMMON_DEFINITIONS>>>
)
target_link_libraries(EngineDependencies
INTERFACE
<<<LIBRARIES>>>
)
A main executable target:
add_executable (EngineApp <<<MAIN_SOURCES>>>)
target_link_libraries(EngineApp PUBLIC EngineDependencies EngineSources)
A test target:
add_executable(Tests PUBLIC <<<TEST_SOURCES>>>)
target_compile_definitions(Tests PRIVATE
<<<TESTING DEFINITIONS>>>
)
target_link_libraries(Tests PUBLIC <<<TESTING LIBRARIES>>>)
target_link_libraries(Tests PUBLIC EngineDependencies EngineSources )
And finally a Benchmarks target:
add_executable(Benchmarks PUBLIC <<<BENCHMARKS_SOURCES>>>)
target_link_libraries(Benchmarks PUBLIC <<<BENCHMARKS LIBRARIES>>>)
target_link_libraries(Benchmarks PUBLIC EngineDependencies)
With this setup the compilation of any of the targets fails because the sources from EngineSources cannot see the files from the libraries defined in EngineDependencies.
I’ve tried many other combinations of the libraries type definition but I haven’t reached a working configuration. For example I’ve tried to link the EngineDependencies as a PUBLIC library of EngineSources and not linking at the lower level (the target level), but I get some duplicates symbols.
Trying to combine INTERFACE for the dependencies and STATIC for the sources I managed to compile the various targets, but cannot change the compile definitions switching the target.
Probably I’m really confused on how to correctly use those tools… but I cannot find any examples for what I’m trying to achieve, and the documentation doesn’t help me.
Hope someone can help me
Best wishes!
Daniele