gpt4 book ai didi

c++ - cmake:我必须按什么顺序指定 TARGET_LINK_LIBRARIES

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:17:09 25 4
gpt4 key购买 nike

我一次又一次地与链接器问题作斗争,因为必须以正确的顺序指定 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 的 undefined reference ”

这真的很烦人,我正在更改库排序,一些 undefined reference 消失了,但出现了新的 undefined reference 。

如何确定正确的顺序?!


编辑:

我通过分别绘制识别库之间的所有依赖关系并适本地排序它们来解决上述问题(因此添加了 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 使用)。

最佳答案

在我的例子中,当我想遵守链接我的库的顺序时,我使用 add_dependencies命令。我的意思是:

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 的一部分,所以我需要看到你的代码才能完整地编写它。

希望对你有帮助!

关于c++ - cmake:我必须按什么顺序指定 TARGET_LINK_LIBRARIES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27170594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com