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)

我得到错误:

  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.

如何执行该生成文件并链接该.a文件?

4

1 回答 1

4

因此,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在尝试链接之前工作并制作了库。

于 2013-07-25T19:08:49.820 回答