如何使用cmake的target_link_libraries来链接匹配glob的库?

时间:2022-09-01 23:42:58

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?

我已经预先构建了第三方库(Boost),我想将其链接到我的目标。它们都存储在一个目录下,如${BOOST_PATH}/lib/libboost_thread。,$ { BOOST_PATH } / lib / libboost_log。a,等等。所以我想做这样的事情:target_link_libraries(${TARGET} PRIVATE“${BOOST_PATH}/libboost* a”)我读过这个文件(GLOB…)可能会被使用,但强烈建议不要使用。我不确定它是否会起作用。为什么?如果不能更改Boost库的目录结构,您将如何解决这个问题?

2 个解决方案

#1


5  

There are two possibilities.

有两种可能性。

  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:

    使用glob是气馁的,因为如果您在这个文件夹中添加一个新的boost库,那么CMake将不会自动检测到这个。您必须手动重新运行CMake以获取新库。但是,没有其他的全局解决方案可以避免这个问题,除非对每个构建调用都做一个概述。所以你所能做的就是简单地列出所有的文件:

    target_link_libraries(${TARGET} PRIVATE
      "${BOOST_PATH}/libboost_filesystem.a"
      "${BOOST_PATH}/libboost_system.a"
      "${BOOST_PATH}/libboost_chrono.a"
      ...
    )
    
  2. 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})
    

#2


12  

Or you could use CMake builtin capabilities to link with Boost, for example:

或者您可以使用CMake构建功能来链接Boost,例如:

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.55.0 REQUIRED thread system log)

include_directories(${Boost_INCLUDE_DIRS})

target_link_libraries(${TARGET} ${Boost_LIBRARIES})

This assumes a standard installation of Boost, with the default directory layout.

这假设标准安装Boost,并使用默认的目录布局。

I do not think globbing is a good idea because you probably do not depend on all Boost compiled libraries, and you would make linking slower for no good reason.

我不认为globbing是一个好主意,因为您可能不依赖于所有Boost编译的库,并且您将没有任何理由的链接慢。

Even if you do, it is still a good idea to list dependencies explicitely.

即使您这样做了,也应该明确列出依赖项。

#1


5  

There are two possibilities.

有两种可能性。

  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:

    使用glob是气馁的,因为如果您在这个文件夹中添加一个新的boost库,那么CMake将不会自动检测到这个。您必须手动重新运行CMake以获取新库。但是,没有其他的全局解决方案可以避免这个问题,除非对每个构建调用都做一个概述。所以你所能做的就是简单地列出所有的文件:

    target_link_libraries(${TARGET} PRIVATE
      "${BOOST_PATH}/libboost_filesystem.a"
      "${BOOST_PATH}/libboost_system.a"
      "${BOOST_PATH}/libboost_chrono.a"
      ...
    )
    
  2. 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})
    

#2


12  

Or you could use CMake builtin capabilities to link with Boost, for example:

或者您可以使用CMake构建功能来链接Boost,例如:

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.55.0 REQUIRED thread system log)

include_directories(${Boost_INCLUDE_DIRS})

target_link_libraries(${TARGET} ${Boost_LIBRARIES})

This assumes a standard installation of Boost, with the default directory layout.

这假设标准安装Boost,并使用默认的目录布局。

I do not think globbing is a good idea because you probably do not depend on all Boost compiled libraries, and you would make linking slower for no good reason.

我不认为globbing是一个好主意,因为您可能不依赖于所有Boost编译的库,并且您将没有任何理由的链接慢。

Even if you do, it is still a good idea to list dependencies explicitely.

即使您这样做了,也应该明确列出依赖项。