Cmake external library .a

Cmake external library .a

本文关键字:library external Cmake      更新时间:2023-10-16

我在这里有一个外部库:

${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在尝试链接之前已经工作并制作了库。