Cmake外部库.a

4

我这里有一个外部库:

${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)

I get the error:

  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文件呢?
1个回答

4

因此,CMake无法确定这里的依赖关系是有道理的:它必须解析makefile并找到输出。你必须告诉它输出结果。我能想到的最好方法是使用custom_command而不是custom target:

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工作并生成了库。


嗨!谢谢你的回答,现在非常接近了!只有一个问题:make[3]: *** No rule to make target '../thirdparty/yaml-cpp/build/libyaml-cpp.a', needed by '../bin/load_balancer_node'. Stop. 有什么想法吗? - Christian Stewart
这应该由add_dependencies(load_balancer_node yaml-cpp)来覆盖。要么是这样,要么第三方makefile将.a文件转储到不同的位置... - IdeaHat
我不确定你使用的是哪个生成器,但如果只是使用make,请尝试“make yaml-cpp”,并查看是否在${CMAKE_SOURCE_DIR}/thirdparty/yaml-cpp/build/libyaml-cpp.a生成.a文件。 - IdeaHat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接