一个CMakeLists.txt中的多个CMake项目

Are*_*ius 4 build cmake visual-studio-2010

我希望有两个项目构建相同的源文件,第二个项目只有一个小子集,以及一些不同的定义和构建标志.

当我尝试这样的事情时:

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} ...)
Run Code Online (Sandbox Code Playgroud)

它最终创建了两个项目,看似正确的源文件等,但由于某些原因,至少在Visual Studio 2010中,两个项目似乎都在链接器标志中定义了MYDEFINE和myflag.

我不确定为什么它似乎适用于文件,但不是标志.

Ale*_*xey 5

首先,必须为可执行文件使用不同的名称如果要向目标添加特定定义,可以使用set_target_properties,因此每个目标都有自己的属性(例如,编译定义).

# 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")
Run Code Online (Sandbox Code Playgroud)

如果要覆盖链接标志,可以将set_target_propertiesLINK_FLAGS 一起使用