C++ 使用Clang 4.1(LLVM 3.1svn)退出时增压测试崩溃

C++ 使用Clang 4.1(LLVM 3.1svn)退出时增压测试崩溃,c++,macos,boost,segmentation-fault,clang,C++,Macos,Boost,Segmentation Fault,Clang,考虑一下这个简单的程序: #include <string> #include <iostream> #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MODULE "MyTest" #include <boost/test/unit_test.hpp> using namespace std; template<char* S> void Test() { BOOST_REQUIRE("B

考虑一下这个简单的程序:

#include <string>
#include <iostream>

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

using namespace std;

template<char* S>
void Test()
{
    BOOST_REQUIRE("Boom!" != string(S));
}

char bang[] = "Bang!";

BOOST_AUTO_TEST_CASE(Boom)
{
    char boom[] = "Boom!";
    Test<bang>();
}
但当使用Apple 4.1(tags/Apple/clang-421.11.65)(基于3.1svn)编译时,它会崩溃

我使用的是Boost 1.51.0,使用BREW安装。使用叮当声重新编译Boost没有帮助

有办法解开这个谜吗

./a.out
Running 1 test case...

*** No errors detected
Segmentation fault: 11

gdb ./a.out
GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug  5 03:00:42 UTC 2012)
...
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .... done

(gdb) r
Starting program: /private/tmp/xx/a.out 
Reading symbols for shared libraries +++............................. done
Running 1 test case...

*** No errors detected

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: 13 at address: 0x0000000000000000
0x0000000100056c56 in boost::unit_test::test_unit::~test_unit ()
(gdb) where
#0  0x0000000100056c56 in boost::unit_test::test_unit::~test_unit ()
#1  0x000000010002435f in boost::unit_test::master_test_suite_t::~master_test_suite_t ()
#2  0x00000001000244dd in boost::unit_test::framework_impl::~framework_impl ()
#3  0x00007fff96179307 in __cxa_finalize ()
#4  0x00007fff9617af57 in exit ()
#5  0x00007fff944897e8 in start ()

lldb a.out
Current executable set to 'a.out' (x86_64).
(lldb) r
Process 71553 launched: '/private/tmp/xx/a.out' (x86_64)
Running 1 test case...

*** No errors detected
Process 71553 stopped
* thread #1: tid = 0x1c03, 0x0000000100056c56 libboost_unit_test_framework.dylib`boost::unit_test::test_unit::~test_unit() + 86, stop reason = EXC_BAD_ACCESS (code=13, address=0x0)
    frame #0: 0x0000000100056c56 libboost_unit_test_framework.dylib`boost::unit_test::test_unit::~test_unit() + 86
libboost_unit_test_framework.dylib`boost::unit_test::test_unit::~test_unit() + 86:
-> 0x100056c56:  lock   
   0x100056c57:  xaddl  %ecx, -8(%rax)
   0x100056c5b:  testl  %ecx, %ecx
   0x100056c5d:  jg     0x100056c68               ; boost::unit_test::test_unit::~test_unit() + 104

clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix

以下内容似乎有效,但您可能需要其他参数

USER (~/tmp)
$ clang++ -pedantic test.cpp -lboost_unit_test_framework-mt 

USER (~/tmp)
$ ./a.out 
Running 1 test case...

*** No errors detected

使用C++11选项重新编译Boost就可以做到这一点

./bootstrap.sh --with-toolset=clang --prefix=/usr/local
sudo ./b2 install toolset=clang cxxflags="-std=c++11 -stdlib=libc++" linkflags="-stdlib=libc++" threading=multi

上次我犯了一个错误,只使用了
/b2安装
,但安装步骤似乎在安装之前构建了一些库。

我遇到了与timlukins相同的问题。 我不得不做
cmake-DBoost\u USE\u STATIC\u LIBS=YES
。但由于我在使用boost_timer编译时遇到其他问题,我发现我的include不正确

我必须替换:

#include <boost/test/included/unit_test.hpp>
#包括
与:

#包括
它在gcc和VS13中运行良好,但在clang中则不行。
如果有帮助的话…

你是否按照我原来的帖子尝试了C++11选项?当你这样做的时候它能工作吗?是的,正是-stdlib=libc++使差异变得非常重要!我使用boost still(1.56.0)的当前版本在约塞米蒂得到了一个segfault 11。我刚刚接受了通过瓶子使用brew进行的默认安装,但不得不执行
brew安装boost--c++11
(使用
brew选项boost
查看所有选项)。但是。。。这最终没有起作用。我不得不使用
cmake-DBoost\u使用\u STATIC\u LIBS=YES
强制cmake静态链接以使其工作。。。仍在调查原因:-(
./bootstrap.sh --with-toolset=clang --prefix=/usr/local
sudo ./b2 install toolset=clang cxxflags="-std=c++11 -stdlib=libc++" linkflags="-stdlib=libc++" threading=multi
#include <boost/test/included/unit_test.hpp>
#include <boost/test/unit_test.hpp>