c++ - Cmake 外部库.a

标签 c++ makefile cmake gnu-make

我这里有一个外部库:

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

相关文章:

c++ - 如何避免对大浮点或 double 值进行舍入?

c++ - 等待 SLOT 完成

c++ - 空格和 makefile

delphi - 为什么 Delphi 决定不必要地重新编译某些文件

c++ - 无法从Github安装C++库

c++ - 是否可以使用字符串或字符来索引数组?

c++ - 使用 std::mutex 避免竞争条件

bluetooth - 蓝牙的 Cmake 和库链接器标志

使用 CMAKE 在 C 中创建静态库

cmake - 替换CMake列表中的值