gpt4 book ai didi

build - 带有 set_target_properties 的 FindPkgConfig 无法用于设置 CFLAGS/LDFLAGS

转载 作者:行者123 更新时间:2023-12-04 02:14:13 32 4
gpt4 key购买 nike

FindPkgConfig 中的

pkg_check_modules 给出 MYLIBRARY_LDFLAGSMYLIBRARY_CFLAGS 是普通的 CMake 列表(带分号分隔符)。

set_target_propertiesset_property 只接受一个字符串。

所以这不起作用,因为它没有扩展列表:

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 在未加引号时扩展为多个字符串,在加引号时扩展为带分号的一个字符串。

我应该如何使用它?

最佳答案

在 CMake 中通过 pkg-config 搜索库的常见工作流程:

# 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-L 选项,CMake 有更合适的命令。使用 XXX_CFLAGS_OTHER 的类似原因。


目前CMake不推荐使用link_directories命令但使用库的绝对路径 target_link_libraries称呼。可以使用 find_library 提取 pkg-config 列出的库的绝对路径命令:

...
# 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})

关于build - 带有 set_target_properties 的 FindPkgConfig 无法用于设置 CFLAGS/LDFLAGS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35457533/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com