Multiple CMake projects in one CMakeLists.txt

I want to have two projects that build the same source files, the second one having only a small subset, as well as several different definition and assembly flags.

When I try something like this:

SET (this_target PROJECT1) PROJECT(${this_target}) ... ADD_EXECUTABLE(#{this_target} ...) SET (this_target PROJECT2) PROJECT(${this_target}) ... add_definitions (-DMYDEFINE) TARGET_LINK_LIBRARIES( ${this_target} -myflag ) ADD_EXECUTABLE(#{this_target} ...) 

As a result, two projects are created, with seemingly correct source files, etc., but for some reason, at least in Visual Studio 2010, both projects seem to define MYDEFINE and myflag in the linker flags.

I'm not sure why this works for files, but not for flags.

+4
source share
2 answers

First, you must use different names for your executables. If you want to add specific definitions to your goals, you can use set_target_properties, so each goal will have its own properties (for example, compilation definitions).

 # compile and link first app add_executable(prg1 ${CommonSources} ${Prg1SpecificSources}) target_link_libraries(prg1 lib1 lib2 lib3) #set target-specific options set_target_properties(prg1 PROPERTIES COMPILE_DEFINITIONS "FOO=BAR1") #... # compile and link second app add_executable(prg2 ${CommonSources} ${Prg2SpecificSources}) target_link_libraries(prg2 lib1 lib2 lib3) #set target-specific options set_target_properties(prg1 PROPERTIES COMPILE_DEFINITIONS "FOO=BAR2") 

If you want to override the binding flags, you can use set_target_properties with LINK_FLAGS

+5
source

I found that including multiple targets in one CMakeLists.txt causes intermittent build failure in Visual Studio 2010 due to counter calls to generate.stamp (although I cannot rule out that I am doing something wrong). Thus, you may have to put targets in different CMakeLists.txt files or find other workarounds.

+1
source

All Articles