• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

c++ adding boost libraires (segmentation fault)


New Member

Messages: 7


I am trying to get a simple c++ console application which compiles and run but the second I add a reference to boost it fails with segmentation fault.

main.cpp - this fails with segmentation fault (if i remove boost references it runs fine)
#include <iostream>
#include <boost/thread.hpp>

using namespace std;
using namespace boost;

int main()
   cout << "Hello World!";
   return 0;
install g++ and boost
pkg install gcc
pkg install boost-all
How to compile
g++ -Wall -std=c++11 -fexceptions -O2  -c "main.cpp" -o "main.o"
g++  -o "main" main.o  -s  -lboost_system -lboost_thread
chmod +x ./main
Segmentation fault (core dumped)
Any ideas why this fails?

Thanks for any help you can get
Last edited by a moderator:



Thanks: 1,184
Messages: 1,775

Boost is linked with libc++, but GCC uses libstdc++. Both are incompatible with each other and your program is linked with both (to libc++ via boost and to libstdc++ through g++) leading to crashes.

The most straightforward solution here is to use Clang to compile your program. FreeBSD already comes with it installed. Just use c++ instead of g++. Unlike with g++ /usr/local is not in Clang's default search paths, so you have to add them too.
c++ -Wall -std=c++11 -fexceptions -O2 -I/usr/local/include -c main.cpp
c++ -o main main.o -s -L/usr/local/lib -lboost_system -lboost_thread