cmake:我必须以哪种顺序指定TARGET_LINK_LIBRARIES

时间:2014-11-27 12:15:46

标签: c++ boost linker cmake

我一次又一次地遇到链接器问题,因为必须以正确的顺序指定TARGET_LINK_LIBRARIES中的所有库。但是我如何确定这个订单呢? 示例:

我有以下库

libA depends on boost
libB depends on postgresql and libA (and therefore on boost)
myTarget uses libA, libB and boost directly (and through libB depends on postgresql)

由于在创建可执行文件时只链接了所有必需的库,因此在链接myTarget(最终可执行文件)时必须指定所有库:

TARGET_LINK_LIBRARIES(${ApplicationName}  

libboost_program_options.a 
libboost_system.a 
libboost_filesystem.a 
libboost_date_time.a 
libboost_regex.a 

# Should include all boost libraries but strangely some libs (the ones above)
# need to be specified "by hand"???
${Boost_LIBRARIES} 

# PostgreSQL stuff
libpq.a
libsoci_core.a
libsoci_postgresql.a
libpq.so

# My libs
libB.a
libA.a

${CMAKE_THREAD_LIBS_INIT} # pthreads, needed by boost
${CMAKE_DL_LIBS} # For libdl.so etc.
) 

由于我链接了boost static,我的CMakeLists.txt也包含

SET(Boost_USE_STATIC_LIBS ON) 

但是,我仍然会收到链接错误,例如"对boost :: re_detail :: perl_matcher的未定义引用或者boost :: date_time :: month_formatter"

这真的很烦人,我正在更改lib排序,一些未定义的引用会消失,但会出现新的未定义引用。

如何识别正确的订购?!


修改

我通过分别识别libs之间的所有依赖关系并对其进行适当排序(因此添加了libboost_log.a)解决了上述问题:

TARGET_LINK_LIBRARIES(${ApplicationName}  
libB.a
libA.a

# PostgreSQL stuff
libpq.a
libsoci_core.a
libsoci_postgresql.a
libpq.so

# Boost
libboost_program_options.a 
libboost_system.a 
libboost_log.a 
libboost_filesystem.a 
libboost_date_time.a 
libboost_regex.a 
# Should include all boost libraries but strangely some libs (the ones above)
# need to be specified "by hand"???
${Boost_LIBRARIES} 

# Lowlevel needed by boost
${CMAKE_THREAD_LIBS_INIT} # pthreads, needed by boost
${CMAKE_DL_LIBS} # For libdl.so etc.
) 

所以订购自上而下。 Top是可执行文件,后跟直接使用的库。接下来是进一步的依赖关系,最后必须添加低级依赖关系(由boost使用)。

1 个答案:

答案 0 :(得分:1)

在我的情况下,当我想尊重链接我的库的顺序时,我使用add_dependencies cmake命令。我的意思是:

find_package(Boost COMPONENTS date_time filesystem system ...)
find_package(PostgreSQL REQUIRED)
IF(Boost_FOUND AND PostgreSQL_FOUND)
  INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
  INCLUDE_DIRECTORIES(${PostgreSQL_INCLUDE_DIRS})

  # ApplicationName -> your_application
  # LIB_A_TARGET -> libA
  # LIB_B_TARGET -> libB

  SET(LIB_A_ALL_DEPS ${Boost_LIBRARIES})
  SET(LIB_B_ALL_DEPS ${LIB_A_ALL_DEPS} ${PostgreSQL_LIBRARIES})

  SET(EXTRA_APP_DEPS  ) # here all your extra libs that they aren't in boost or postgre

  SET(YOUR_APP_ALL_DEPS ${LIB_A_ALL_DEPS} ${LIB_B_ALL_DEPS} ${EXTRA_APP_DEPS})

  # Here'll be all your ADD_EXECUTABLE, ADD_LIBRARY code

  TARGET_LINK_LIBRARIES(${LIB_A_TARGET} ${LIB_A_ALL_DEPS})
  ADD_DEPENDENCIES(${LIB_A_TARGET} ${LIB_A_ALL_DEPS})

  TARGET_LINK_LIBRARIES(${LIB_B_TARGET} ${LIB_B_ALL_DEPS})
  ADD_DEPENDENCIES(${LIB_B_TARGET} ${LIB_B_ALL_DEPS})

  TARGET_LINK_LIBRARIES(${ApplicationName} ${YOUR_APP_ALL_DEPS})
  ADD_DEPENDENCIES(${ApplicationName} ${YOUR_APP_ALL_DEPS})
ENDIF()

我没有测试过此代码,但我会或多或少地执行此操作。我知道这个例子只是可能的CMakeLists.txt的一部分所以我需要看看你的代码完全写它。

我希望它可以帮到你!