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

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

问题描述

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

  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)


b $ b

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

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

工作正常 - 但如果我编译相同的Ubuntu似乎忽略子目录的CMakeLists和抱怨:

  / usr / bin /ld.bfd.real:找不到-lmylib 

我使用Clang进行编译。解决方案是将GLOBAL添加到您的add_library调用中,以使其对父CMakeLists.txt可见

$ b $ b

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)

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

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

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

I'm using Clang for compilation.

解决方案

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天全站免登陆