【发布时间】:2015-01-26 01:07:17
【问题描述】:
由于必须以正确的顺序指定 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 的引用”
这真的很烦人,我正在更改库排序,一些未定义的引用消失了,但出现了新的未定义引用。
如何识别正确的顺序?!
编辑:
我通过分别绘制识别库之间的所有依赖关系并对其进行适当排序来解决上述问题(因此添加了 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.
)
所以排序是自上而下的。顶部是可执行文件,后面是直接使用的库。接下来是更多的依赖,最后必须添加低级依赖(由 boost 使用)。
【问题讨论】:
-
UNIXen 有
tsort(howtounix.info/man/FreeBSD/man1/tsort.1) 可以为你做这个拓扑排序,如果你给它一个直接依赖的列表