具有set_target_properties的FindPkgConfig无法用于设置CFLAGS / LDFLAGS [英] FindPkgConfig with set_target_properties is unusable for setting CFLAGS/LDFLAGS
问题描述
pkg_check_modules
给出了 MYLIBRARY_LDFLAGS
和 MYLIBRARY_CFLAGS
是普通的CMake列表(带有分号分隔符)。
pkg_check_modules
from FindPkgConfig gives MYLIBRARY_LDFLAGS
and MYLIBRARY_CFLAGS
that are ordinary CMake lists (with semicolon-separator).
set_target_properties
和 set_property
仅接受一个字符串。
The set_target_properties
and set_property
accept just one string.
因此,该操作无效,因为它不会扩展列表:
So that doesn't work because it doesn't expand the list:
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY LINK_FLAGS ${MYLIBRARY_LDFLAGS})
这与分号里面的内容相同:
This gives same thing with the semicolons inside:
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY LINK_FLAGS "${MYLIBRARY_LDFLAGS}")
set_target_properties
在不加引号的情况下扩展为多个字符串,在加引号的情况下扩展为一个带分号的字符串。
set_target_properties
expands into multiple strings when unquoted and into one string with semicolons when quoted.
我应该如何使用呢?
推荐答案
包含CMake中pkg-config搜索的库的通用工作流程:
Common workflow with libraries searched by pkg-config within CMake:
# Use pkg-config for search library `xxx`.
pkg_check_modules(XXX xxx)
# Build target which uses given library
# The only command which has no target-oriented equivalent.
# But see comments after the code.
link_directories(${XXX_LIBRARY_DIRS})
# Two global commands belows can be replaced by target-oriented equivalent
# after creation of the target.
include_directories(${XXX_INCLUDE_DIRS})
add_compile_options(${XXX_CFLAGS_OTHER})
# Create target
add_executable(my_exe ...) # Or add_library()
# The only target-oriented command, which has no global equivalent.
target_link_libraries(my_exe ${XXX_LDFLAGS_OTHER}) # It is OK to have link flags here
target_link_libraries(my_exe ${XXX_LIBRARIES}) # Can be combined with previous call.
请注意,我们改用 XXX_LDFLAGS_OTHER
变量 XXX_LDFLAGS
之一。这是因为 XXX_LDFLAGS
包括 -l </ code>和
-L
选项,CMake具有更合适的命令。使用 XXX_CFLAGS_OTHER
的原因与此类似。
Note, that we use XXX_LDFLAGS_OTHER
variable instead of XXX_LDFLAGS
one. This is because XXX_LDFLAGS
includes -l
and -L
options for which CMake has more suitable commands. Similar reason about usage of XXX_CFLAGS_OTHER
.
当前,CMake不会建议不要使用 link_directories 命令,但要使用库的绝对路径在 target_link_libraries 调用中。可以使用来提取pkg-config列出的库的绝对路径。 find_library 命令:
Currently, CMake doesn't recommend to use link_directories command but use absolute paths to libraries in target_link_libraries call. One can extract absolute paths to libraries, listed by pkg-config, with use of find_library command:
...
# Instead of using `link_directories`, collect absolute paths to libraries.
set(XXX_LIBS_ABSOLUTE)
foreach(lib ${XXX_LIBRARIES})
# Choose name of variable, which will contain result of `find_library`
# for specific library.
set(var_name XXX_${lib}_ABS)
# Search library under dirs, returned by pkg-config.
find_library(${var_name} ${lib} ${XXX_LIBRARY_DIR})
list(APPEND XXX_LIBS_ABSOLUTE ${${var_name}})
endforeach()
# Instead of `target_link_libraries(my_exe ${XXX_LIBRARIES})`
target_link_libraries(my_exe ${XXX_LIBS_ABSOLUTE})
这篇关于具有set_target_properties的FindPkgConfig无法用于设置CFLAGS / LDFLAGS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!