1

I have wrote a simple code which uses c++11 thread. It compiles correct but when I try to run it I have the following error:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

I am using gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1). This is the output of the makefile which I am using

g++ -std=c++11 -DHAVE_CONFIG_H -I. -I../../../..  -I../../../.. -I/usr/local_machine/openmpi-1.6.5/include -pthread -DMPIPP_H -DENABLE_MPI=1    -O3 -Wall -DNDEBUG -funroll-loops -finline-functions -pthread -fomit-frame-pointer -ffast-math -mfpmath=sse -msse3 -MT threadtest-threadtest.o -MD -MP -MF .deps/threadtest-threadtest.Tpo -c -o threadtest-threadtest.o `test -f 'threadtest.cc' || echo './'`threadtest.cc
mv -f .deps/threadtest-threadtest.Tpo .deps/threadtest-threadtest.Po
/bin/bash ../../../../libtool  --tag=CXX   --mode=link g++ -std=c++11  -O3 -Wall -DNDEBUG -funroll-loops -finline-functions -pthread -fomit-frame-pointer -ffast-math -mfpmath=sse -msse3     -o threadtest threadtest-threadtest.o   -pthread -L/usr/local_machine/openmpi-1.6.5/lib -lmpi -ldl -lm -Wl,--export-dynamic -lrt -lnsl -lutil -lm -ldl ../../../../lib/libdunecommon.la -lm 
libtool: link: g++ -std=c++11 -O3 -Wall -DNDEBUG -funroll-loops -finline-functions -pthread -fomit-frame-pointer -ffast-math -mfpmath=sse -msse3 -o threadtest threadtest-threadtest.o -pthread -Wl,--export-dynamic  -L/usr/local_machine/openmpi-1.6.5/lib /usr/local_machine/openmpi-1.6.5/lib/libmpi.so -lrt -lnsl -lutil -ldl ../../../../lib/.libs/libdunecommon.a -llapack -lblas -lgfortran -lquadmath -lm -pthread -Wl,-rpath -Wl,/usr/local_machine/openmpi-1.6.5/lib -Wl,-rpath -Wl,/usr/local_machine/openmpi-1.6.5/lib

As you can see there is the flag -pthread. What am I doing wrong? Because if I compile a simple code as

g++ threadstest.cc -o threadstest -pthread -std=c++11

everything works fine therefore in my makefile there is something which interferes with -pthread but I don't understand what it is.

Additional information: a workaround which works is to add -Wl,--no-as-needed at the linking stage.

Anton
  • 6,349
  • 1
  • 25
  • 53
Marco Agnese
  • 349
  • 4
  • 15
  • are you supposed to use both c++11 threading and mpi together? – pqnet Aug 08 '14 at 15:04
  • I am adding an hybrid thread-MPI management in a FEM framework – Marco Agnese Aug 08 '14 at 15:21
  • possible duplicate of [Compiling multithread code with g++ (-Wl,--no-as-needed NOT working)](http://stackoverflow.com/questions/22593658/compiling-multithread-code-with-g-wl-no-as-needed-not-working) – Anton Aug 08 '14 at 15:52

1 Answers1

1

the answer for similar question "Compiling multithread code with g++ (-Wl,--no-as-needed NOT working)"

is

-pthread is a flag for the compiler, not the linker, the right one for the linker is -lpthread

Community
  • 1
  • 1
Anton
  • 6,349
  • 1
  • 25
  • 53
  • Please don't repeat that quote about -pthread being the wrong option. Calling gcc with -pthread is normally the right thing to do, there just happens to be a bug in this particular version of the toolchain in ubuntu. – Marc Glisse Aug 11 '14 at 17:33
  • thanks, can you please refer to another answer or a bug-report or post your own answer with such a link? – Anton Aug 11 '14 at 17:38
  • gcc's doc says about -pthread: "This option sets flags for both the preprocessor and linker" and the question you link to has a link to Ubuntu's bug tracker, what need is there for more reference... – Marc Glisse Aug 11 '14 at 21:30
  • @MarcGlisse, it's hard to say whether this bug is related to this question since the question uses `g++` though `libtool` not directly. And `libtool` threats `-l` arguments specially. So, it will not understand `-pthread` which is parsed only by compiler front-end and passed to underlying linker as -lpthread – Anton Aug 20 '14 at 12:27