gpt4 book ai didi

c++ - Cmake 外部库.a

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:43 24 4
gpt4 key购买 nike

我这里有一个外部库:

${PROJECT_SOURCE_DIR}/thirdparty/yaml-cpp/

它是由 Makefile 制作的:thirdparty/Makefile。我正在像这样执行该 makefile:

add_custom_target(
yaml-cpp
COMMAND make
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/thirdparty
)

然后我尝试链接构​​建到 thirdparty/yaml-cpp/build/libyaml-cpp.a 的库。 这是不起作用的部分:

target_link_libraries(load_balancer_node ${CMAKE_SOURCE_DIR}/thirdparty/yaml-cpp/build/libyaml-cpp.a)

我得到错误:

  Target "yaml-cpp" of type UTILITY may not be linked into another target.
One may link only to STATIC or SHARED libraries, or to executables with the
ENABLE_EXPORTS property set.

我如何执行该 makefile 并链接 .a 文件?

最佳答案

因此,cmake 无法确定此处的依赖关系是有道理的:它必须解析 makefile 并找到输出。你必须告诉它输出某人。据我所知,最好的方法是使用 custom_command 而不是自定义目标:

add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/thirdparty/yaml-cpp/build/libyaml-cpp.a
COMMAND make
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/thirdparty)
add_custom_target(
yaml-cpp
DEPENDS ${CMAKE_SOURCE_DIR}/thirdparty/yaml-cpp/build/libyaml-cpp.a)
...
add_dependencies(load_balancer_node yaml-cpp)
target_link_libraries(load_balancer_node ${CMAKE_SOURCE_DIR}/thirdparty/yaml-cpp/build/libyaml-cpp.a)

虽然我遇到了链接器问题(愚蠢的 Windows 机器),但是 cmake 在尝试链接之前工作并创建了库。

关于c++ - Cmake 外部库.a,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17865285/

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