CMake:链接到具有依赖项的导入库失败 [英] CMake: Linking to an imported library with dependencies fails

查看:41
本文介绍了CMake:链接到具有依赖项的导入库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 CMakeLists.txt 的子目录,它应该使用 make 编译一个库并将结果作为导入的库导出到父目录:

I have a subdirectory with a CMakeLists.txt which should compile a library using make and export the result as an imported library to the parent directory:

set(static_lib ${CMAKE_CURRENT_BINARY_DIR}/lib/mylib.a)

add_custom_command(
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT ${static_lib}
    COMMAND make
    COMMAND make install PREFIX=${CMAKE_CURRENT_BINARY_DIR}
)

add_custom_target(compile_mylib DEPENDS ${static_lib})

add_library(mylib STATIC IMPORTED)
set_property(TARGET mylib PROPERTY IMPORTED_LOCATION ${static_lib})
add_dependencies(mylib compile_mylib)

父目录中的 CMakeLists.txt 如下所示:

The CMakeLists.txt in the parent directory looks like this:

add_subdirectory(deps/mylib)
add_executable(mybin source.c)
target_link_libraries(mybin mylib)

在 OSX 上这工作得很好 - 但如果我在 Ubuntu 上编译它似乎忽略了子目录的 CMakeLists 并抱怨:

On OSX this works just fine - but if I compile the same on Ubuntu it seems to ignore the subdirectory's CMakeLists and complains:

/usr/bin/ld.bfd.real: cannot find -lmylib

我正在使用 Clang 进行编译.

I'm using Clang for compilation.

推荐答案

解决方案是将 GLOBAL 添加到您的 add_library 调用中,以便它对父级 CMakeLists.txt 可见

The solution is to add GLOBAL to your add_library call so that it is visible to the parent CMakeLists.txt

这篇关于CMake:链接到具有依赖项的导入库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆