• 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)

EverydayDiesel

Member


Messages: 21

#1
Hello,

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)
C++:
#include <iostream>
#include <boost/thread.hpp>

using namespace std;
using namespace boost;

int main()
{
   cout << "Hello World!";
   return 0;
}
install g++ and boost
Code:
pkg install gcc
pkg install boost-all
How to compile
Code:
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
./main
Segmentation fault (core dumped)
Any ideas why this fails?

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

tobik@

Daemon
Developer

Thanks: 1,327
Messages: 1,908

#2
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.
Code:
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
 
Top