问题描述
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})
这与里面的分号相同:
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
includes -l
和 -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屋!
更多推荐
[db:关键词]
发布评论