我在使用 cmake 生成器表达式TARGET_NAME_IF_EXISTS时遇到问题。有了这个CMakeLists.txt
:
cmake_minimum_required(VERSION 3.13.0)
option(SLIB_BUILD_STATIC "" ON)
project(slib VERSION 1.0)
add_library(slibObjects OBJECT main.c)
add_library(slib SHARED $<TARGET_OBJECTS:slibObjects>)
if (SLIB_BUILD_STATIC) # Can this if() be replaced with a GenExp?
add_library(slibStatic STATIC $<TARGET_OBJECTS:slibObjects>)
endif()
set_target_properties(
slib
$<TARGET_NAME_IF_EXISTS:slibStatic> # This GenExp doesn't get reduced
PROPERTIES
VERSION ${SLIB_VERSION}
SOVERSION ${SLIB_VERSION_MAJOR}
)
我明白了
CMake Error at CMakeLists.txt:12 (set_target_properties):
set_target_properties Can not find target to add properties to:
$<TARGET_NAME_IF_EXISTS:slibStatic>
我希望set_target_properties
根据是否SLIB_BUILD_STATIC
设置减少到其中之一:
set_target_properties( slib slibStatic PROPERTIES ...)
set_target_properties( slib PROPERTIES ...)
我究竟做错了什么?