我有一个 CMake 项目,它使用来自 boost 的 unit_test_framework 和文件系统库。我正在尝试使用boost::timer::auto_cpu_timer
但无法将其链接到 Ubuntu 12.04。Win32 上的构建工作正常。
有问题的代码。
#include <boost/timer/timer.hpp>
long some_function()
{
boost::timer::auto_cpu_timer t;
...rest omitted...
}
我使用手工构建的 boost 1.52.0(完整;版本化;d、mt、mt-d、mt-s、mt-sd、s、sd;静态和共享)。
我在主 CMakeLists.txt 中使用以下配置(发现提升;工作正常)
set (Boost_USE_STATIC_LIBS ON)
set (Boost_USE_MULTITHREADED ON)
if (MSVC)
set (Boost_USE_STATIC_RUNTIME OFF)
else ()
set (Boost_USE_STATIC_RUNTIME ON)
endif ()
find_package (
Boost ${BOOST_VERSION_MAJOR}.${BOOST_VERSION_MINOR}.${BOOST_VERSION_MICRO} COMPONENTS
system filesystem unit_test_framework chrono timer REQUIRED
)
if (Boost_FOUND)
include_directories (${Boost_INCLUDE_DIRS})
endif ()
在可执行文件的子项目中,我使用以下说明。如果没有 chrono,Win32 项目将无法链接。我还尝试使用 rt 和 pthread 库(来自发行版的 pthread),但无济于事。
target_link_libraries (
<exename> ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${Boost_CHRONO_LIBRARY} ${Boost_TIMER_LIBRARY}
<other company libs>
)
正是结果命令给了我错误。我添加了该-Q -v -Wl,-t
部分以查看链接器正在尝试做什么。(boost 也是用-std=c++0x
标志编译的,否则文件系统将无法工作)
/usr/bin/c++ -Q -v -Wl,-t -Wextra -fPIC -std=c++0x -Wextra -fPIC -std=c++0x -D_DEBUG \
<our object files> -o ../bin/<exe name> -rdynamic \
<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_system-gcc46-mt-sd-1_52.a \
<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_filesystem-gcc46-mt-sd-1_52.a \
<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a \
<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_timer-gcc46-mt-sd-1_52.a \
<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_chrono-gcc46-mt-sd-1_52.a \
<our other static libs>
来自链接器的日志。
/usr/bin/ld: mode elf_i386
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o
/usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o
/lib/i386-linux-gnu/libpthread.so.0
-lrt (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/librt.so)
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_system-gcc46-mt-sd-1_52.a)error_code.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_filesystem-gcc46-mt-sd-1_52.a)operations.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_filesystem-gcc46-mt-sd-1_52.a)path.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)framework.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)progress_monitor.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)results_collector.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)results_reporter.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)test_tools.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)unit_test_log.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)unit_test_main.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)unit_test_monitor.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)unit_test_parameters.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)unit_test_suite.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)xml_log_formatter.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)xml_report_formatter.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)compiler_log_formatter.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)debug.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)execution_monitor.o
(<some dir>/libs/boost_1_52_0/stage_x86/lib/libboost_unit_test_framework-gcc46-mt-sd-1_52.a)plain_report_formatter.o
<our object files>
-lstdc++ (/usr/lib/gcc/i686-linux-gnu/4.6/libstdc++.so)
-lm (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libm.so)
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.6/libgcc_s.so)
/lib/i386-linux-gnu/libc.so.6
(/usr/lib/i386-linux-gnu/libc_nonshared.a)elf-init.oS
(/usr/lib/i386-linux-gnu/libc_nonshared.a)stat64.oS
(/usr/lib/i386-linux-gnu/libc_nonshared.a)lstat64.oS
(/usr/lib/i386-linux-gnu/libc_nonshared.a)stack_chk_fail_local.oS
/lib/i386-linux-gnu/ld-linux.so.2
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.6/libgcc_s.so)
/usr/lib/gcc/i686-linux-gnu/4.6/crtend.o
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crtn.o
似乎它没有使用计时器库中的任何东西。这是我不明白的部分。有没有人遇到过类似的问题?我是否缺少依赖项?产生的错误很简单:
<some file>.cpp:(.text+0x38): undefined reference to `boost::timer::auto_cpu_timer::auto_cpu_timer(short)'
<some file>.cpp:(.text+0x4e8): undefined reference to `boost::timer::auto_cpu_timer::~auto_cpu_timer()'
<some file>.cpp:(.text+0x543): undefined reference to `boost::timer::auto_cpu_timer::~auto_cpu_timer()'
我希望我没有忘记提及任何事情。提前感谢您的任何建议。