How to use cmake's target_link_libraries to link libraries matching a glob?

ⅰ亾dé卋堺 提交于 2019-12-03 08:56:07

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:

    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})
    

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

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.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!