如何使用cmake的target_link_libraries链接与glob匹配的库? [英] How to use cmake's target_link_libraries to link libraries matching a glob?

查看:223
本文介绍了如何使用cmake的target_link_libraries链接与glob匹配的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经预构建了第三方库(Boost),我想链接到我的目标。它们全部存储在一个目录下,例如$ {BOOST_PATH} /lib/libboost_thread.a,$ {BOOST_PATH} /lib/libboost_log.a等。
所以我想这样做:
target_link_libraries($ {TARGET} PRIVATE $ {BOOST_PATH} /libboost*.a)
我读过 FILE(GLOB ...)可能会使用,但强烈建议不要这样做。而且我不确定它是否会奏效。为什么?
如果无法更改Boost库的目录结构,将如何解决此问题?

I have prebuilt thirdparty libraries (Boost) which I want to link to my target. All of them is stored under one directory like ${BOOST_PATH}/lib/libboost_thread.a, ${BOOST_PATH}/lib/libboost_log.a, etc. So I would like to do something like this: target_link_libraries(${TARGET} PRIVATE "${BOOST_PATH}/libboost*.a") I've read that FILE(GLOB...) might be used but strongly discouraged. And I am not sure that it would work at all. Why? How would you solve this problem if you cannot change the directory structure of the Boost libraries?

推荐答案

有两个


  1. 不建议使用glob,因为如果将新的boost库添加到此文件夹中,则CMake不会自动检测到。您将必须手动重新运行CMake才能拾取新库。但是,除了以某种方式在每个构建调用上进行glob之外,没有其他globing解决方案可以防止此问题。因此,您只需列出所有文件即可:

  1. Using glob is discouraged because if you add a new boost library into this folder, then CMake will not automatically detect this. You will have to rerun CMake manually to pick up the new library. However, no other globbing solution would prevent this problem, except somehow doing a glob upon every build call. So what you could do is simply list all the files:

target_link_libraries(${TARGET} PRIVATE
  "${BOOST_PATH}/libboost_filesystem.a"
  "${BOOST_PATH}/libboost_system.a"
  "${BOOST_PATH}/libboost_chrono.a"
  ...
)


  • 第二个解决方案是使用您建议的内容。遵循这些原则的方法应该起作用:

  • The second solution is to use what you proposed. Something along these lines should work:

    file(GLOB LIBS "${BOOST_PATH}/libboost*.a")
    target_link_libraries(${TARGET} PRIVATE ${LIBS})
    


  • 这篇关于如何使用cmake的target_link_libraries链接与glob匹配的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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