在Mac OS X 10.8上编译并使用boost 1.51.0

chr*_*olz 2 c++ macos boost c++11 libc++

所以我花了最近20个小时尝试在OS X 10.8下运行,我终于让它编译没有错误,但是当我尝试编译一个使用Boost.test的测试用例时,我又回来了在一个受伤的世界里.我应该提一下,我自己编译boost而不是使用二进制文件的原因是因为我想使用c ++ 11和libc ++.

当我编译boost时,我这样称为b2:

./b2 toolset=clang cxxflags="-std=c++11 -stdlib=libc++" linkflags="-std=c++11 -stdlib=libc++" link=static
Run Code Online (Sandbox Code Playgroud)

它编译所有文件.然后我有这段代码

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Addition
#include <boost/test/unit_test.hpp>

int addition(int i, int j)
{
    return i + j;
}

BOOST_AUTO_TEST_CASE(universeInOrder)
{
    BOOST_CHECK(addition(2, 2) == 4);
}
Run Code Online (Sandbox Code Playgroud)

我尝试编译

clang++ -std=c++11 -stdlib=libc++ -g -Wall -v -I/Users/cb/Downloads/boost_1_51_0 tests/arithmetic.cpp -o tests/arithmetic /Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a`
Run Code Online (Sandbox Code Playgroud)

它失败了,给我这个错误:

"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o tests/arithmetic /var/folders/pg/4wcxn1j12c3188vqrv0x4w9r0000gn/T/arithmetic-UFmO1B.o     /Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a -lc++ -lSystem     /usr/bin/../lib/clang/4.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
      _main in arithmetic-UFmO1B.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

起初,我认为这是因为没有为64位编译boost,所以我试着告诉b2专门做这个,但它没有任何区别,我也认为它在OS X上默认编译为64位.

关于它为什么失败以及我如何运作的任何想法?

Jan*_*dec 5

它看起来像测试的糟糕编译.你要求了

#define BOOST_TEST_DYN_LINK
Run Code Online (Sandbox Code Playgroud)

但是你要链接boost.test的静态版本

/Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a
Run Code Online (Sandbox Code Playgroud)

和IIRC这个库的静态和动态版本之间存在差异.因此,要么链接库的动态版本(具有.so扩展名的动态版本),要么删除该定义.