9

Consider this simple program:

#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>();
}

I'm on Mac OS X v10.8.2 (Mountain Lion) and have XCode 4.5 installed.

The program works when compiled with GCC, for example,

gcc test.cpp -lboost_unit_test_framework-mt -lstdc++

but it crashes when compiled with Apple Clang 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)

clang -std=c++11 -stdlib=libc++ -lc++ test.cpp -lboost_unit_test_framework-mt

I'm using Boost 1.51.0, installed using BREW. Recompiling Boost using Clang does not help.

Is there a solution to this mystery?

./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

3 Answers 3

10

Recompiling Boost with the C++11 options does the trick.

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

I made a mistake of using just ./b2 install last time but the install step appears to build some libraries before installing them.

2
  • Great catch! I was getting a segfault 11 using the current version of boost still (1.56.0) and on Yosemite. I had just accepted the default install using brew via bottle - but instead had to do a brew install boost --c++11 (use brew options boost to see all options).
    – timlukins
    Dec 8, 2014 at 20:04
  • HOWEVER... this ultimately didn't work. I had to use cmake .. -DBoost_USE_STATIC_LIBS=YES to compel cmake to link in statically for it to work... Still investigating why:-(
    – timlukins
    Dec 8, 2014 at 21:16
1

I had the same issue than timlukins. I had to do cmake .. -DBoost_USE_STATIC_LIBS=YES. But as I had other issues compiling with boost_timer for instance, I found that it was my include that was not correct.

I had to replace :

#include <boost/test/included/unit_test.hpp>

with:

#include <boost/test/unit_test.hpp>

It was working fine with gcc and VS13, but not with clang. If it can help...

0

The following seems to work, but you may need the other arguments.

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

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

*** No errors detected
2
  • Did you try the C++11 options as per my original post? Does it work when you do? Oct 2, 2012 at 18:51
  • 1
    Yes, it's the -stdlib=libc++ that makes the difference
    – sunkencity
    Oct 2, 2012 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.