如何在CMake的set_target_properties()函数中将多个项目添加到同一属性?

时间:2019-11-13 13:21:02

标签: build cmake

set_target_properties(MyTarget PROPERTIES IMPORTED_LOCATION_RELEASE 
                       ${_IMPORT_PREFIX}/lib/mysharedlibrary.so)

将添加该共享库。但是,我希望添加多个共享库。

我尝试了以下操作:

set_target_properties(MyTarget PROPERTIES IMPORTED_LOCATION_RELEASE 
                       "${_IMPORT_PREFIX}/lib/mysharedlibrary_1.so" 
                       "${_IMPORT_PREFIX}/lib/mysharedlibrary_2.so")

但这不起作用。

2 个答案:

答案 0 :(得分:1)

IMPORTED_LOCATION_RELEASE指定单个文件的位置。您可以创建两个目标,然后与它们链接:

add_library(mysharedlibrary_1 SHARED IMPORTED)
set_target_properties(mysharedlibrary_1 PROPERTIES 
         IMPORTED_LOCATION_RELEASE ${_IMPORT_PREFIX}/lib/mysharedlibrary_1.so)

add_library(mysharedlibrary_2 SHARED IMPORTED)
set_target_properties(mysharedlibrary_2 PROPERTIES
          IMPORTED_LOCATION_RELEASE ${_IMPORT_PREFIX}/lib/mysharedlibrary_2.so)

add_library(MyTarget INTERFACE)
target_link_libraries(MyTarget INTERFACE mysharedlibrary_1 mysharedlibrary_2)

答案 1 :(得分:0)

由于set_target_properties()可以在一个调用中设置多个属性,因此必须使IMPORTED_LOCATION_RELEASE之后的参数显示为一个参数。为此,可以将库连接到以分号分隔的列表中:

set_target_properties(MyTarget PROPERTIES IMPORTED_LOCATION_RELEASE
    "${_IMPORT_PREFIX}/lib/mysharedlibrary_1.so;${_IMPORT_PREFIX}/lib/mysharedlibrary_2.so"
)

或者,您可以使用set()命令为您创建列表。您可以在变量中设置这些库,然后在set_target_properties()调用中扩展list变量:

set(MY_SHARED_LIBS
    "${_IMPORT_PREFIX}/lib/mysharedlibrary_1.so" 
    "${_IMPORT_PREFIX}/lib/mysharedlibrary_2.so"
)

set_target_properties(MyTarget PROPERTIES 
    IMPORTED_LOCATION_RELEASE "${MY_SHARED_LIBS}"
)

第三个选择是使用set_property(),它允许您将每个库作为单独的参数列出,这可能有助于提供更正确的格式:

set_property(TARGET MyTarget PROPERTY
    IMPORTED_LOCATION_RELEASE 
    ${_IMPORT_PREFIX}/lib/mysharedlibrary_1.so
    ${_IMPORT_PREFIX}/lib/mysharedlibrary_2.so
)