c – cmake:我必须以哪种顺序指定TARGET_LINK_LIBRARIES
发布时间:2020-12-16 07:10:22 所属栏目:百科 来源:网络整理
导读:我一次又一次地遇到链接器问题,因为必须以正确的顺序指定TARGET_LINK_LIBRARIES中的所有库.但是我如何确定这个订单呢?例: 我有以下库 libA depends on boostlibB depends on postgresql and libA (and therefore on boost)myTarget uses libA,libB and boo
我一次又一次地遇到链接器问题,因为必须以正确的顺序指定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使用). 解决方法
在我的情况下,当我想尊重链接我的库的顺序时,我使用
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的一部分所以我需要看看你的代码完全写它. 我希望它对你有所帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |