【发布时间】:2017-07-05 13:15:55
【问题描述】:
我正在静态链接 boost。当我这样做时,我得到了一些未定义的参考错误(如下)。
[100%] Linking CXX executable infiniteTests
/home/wdog/src/protos/infinite/libinfinite.a(ServiceDiscovery.cpp.o): In function `boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::simple_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::ostream&)':
/home/wdog/src/3rdp/boost_1_63_0/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/home/wdog/src/3rdp/boost_1_63_0/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
collect2: error: ld returned 1 exit status
当我动态链接时,引用可能存在,并且构建链接成功。
我需要能够静态构建它;项目要求。
我正在使用 cmake 构建。我的链接目录是这样排序的:
link_directories(
$ENV{PROJECT_LIB_DIR}
$ENV{BOOST_LIBRARY_DIR}
$ENV{HIREDIS_LIB_DIR}
$ENV{LIBEVENT_LIB_DIR}
$ENV{PROJECT_ROOT}
/usr/lib
/usr/local/lib
)
我的目标链接库定义为:
target_link_libraries(
${sThisProject}
${Boost_LIBRARIES}
libhiredis.a
libevent.a
libevent_core.a
libevent_pthreads.a
libssl.so
libcrypto.so
libpthread.so.0
)
这是我找到 boost 的 CMakeLists.txt 条目:
find_package( Boost 1.63 EXACT REQUIRED COMPONENTS system chrono date_time filesystem program_options regex thread REQUIRED )
我还设置了以下内容:
SET (Boost_NO_SYSTEM_PATHS ON)
SET (Boost_USE_STATIC_LIBS ON)
SET (Boost_USE_MULTITHREADED ON)
SET (Boost_USE_STATIC_RUNTIME ON)
我已检查我的 1.63 版本是否已成功构建适当的静态库 - libboost_date_time.a - 它存在于 ${BOOST}/stage/lib/
据我了解,如果链接器在正确路径之前在系统安装中发现 boost 静态库,则可能会发生这种情况。
Cmake 似乎找到了 boost 1_63 的正确路径:
(构建期间的输出:)
-- Boost version: 1.63.0
-- Found the following Boost libraries:
-- system
-- chrono
-- date_time
-- filesystem
-- program_options
-- regex
-- thread
我运行fedora 25,所以我系统安装的boost版本是1_60,所以为了避免上述问题,我完全卸载了它:
sudo dnf remove boost boost-atomic boost-chrono boost-container boost-context boost-coroutine boost-date-time boost-devel boost-filesystem boost-graph boost-iostreams boost-locale boost-log boost-math boost-program-options boost-python boost-random boost-regex boost-serialization boost-signals boost-system boost-test boost-thread boost-timer boost-type_erasure boost-wave
我在源代码树的第 3 方目录中构建了 1.63,但没有安装。它是使用以下命令构建的:
./bootstrap.sh
./b2 -j8
我将链接器输出设置为:
SET (CMAKE_EXE_LINKER_FLAGS -v)
并通过以下输出确定我正在链接正确的静态库:
-o
...
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_system.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_chrono.a
**/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_date_time.a**
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_filesystem.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_program_options.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_regex.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_thread.a
...
我的想法已经用完了;还有什么我应该检查的吗?
【问题讨论】:
-
显示导致错误的完整链接器命令行。
libinfinite.a需要在命令行中的 boost 库之前。 -
libinfinite.a 需要在命令行中的 boost 库之前 - 这解决了问题 - 谢谢!
标签: c++ boost cmake static-linking