Compiling multithread code with Code::Blocks GNU compiler

c++, c++11, compilation, linux, multithreading

Solution

Your first problem is that `-lpthread` is a linker option, so it belongs on the linker line (the second command) rather than the compilation line. (Note that the order of the parameters can matter; I got it to work by putting `-lpthread` on last. I also tried using `-pthread` instead of `-lpthread`, which did appear to work and was also less sensitive to where it was put on the linker line. But again, it's a linker option, not a compile option.)

After fixing that, I was able to get your program to compile and run, but it exited with a different exception: `terminate called without an active exception`. To fix this problem, call `thread_fct.join();` in `main()`. (All threads must be joined, detached, or moved-from before they go out of scope, or your program will abort.)

Problem

The error I am trying to use std::thread but I have this error when I try to run it. terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted Aborted (core dumped) Result of my research There are a lot of questions about it and every answer say similar things : I have to build with "-pthread" or "-lpthread". Some also say to add "-Wl,--no-as-needed". Link Link Link Link Link Link Link I tried a lot of things but none worked. Details I am compiling with Code::Blocks 12.11, GNU GCC Compiler on Lubuntu. In the compiler settings menu, compiler flags I have checked "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" and under other options I wrote what the answer was saying, here is an example ``` -pthread -Wl,--no-as-needed ``` Here is the build log I have (I am not sure if it is important) ``` g++ -Wall -fexceptions -std=c++11 -g -pthread -Wl,--no-as-needed -std=c++11 -I../DeskManagerDll -I/usr/include/X11/extensions -I/usr/include/X11 -c /home/julien/Documents/test/main.cpp -o obj/Debug/main.o g++ -L/home/julien/Documents/DeskManagerDll -L-L/usr/lib/i386-linux-gnu -o bin/Debug/test obj/Debug/main.o -L/usr/X11R6/lib -lX11 -lXext -lpthread -Wl,--no-as-needed /home/julien/Documents/DeskManagerDll/bin/Debug/libDeskManagerDll.so Output size is 187,15 KB ``` My question What am I doing wrong ? What did I miss ? Edit I made a very simple program to exclude any other problem. ``` #include <thread> void test() { } int main() { std::thread thread_fct (test); return 0; } ``` The build log with this program: ``` g++ -Wall -fexceptions -std=c++11 -g -pthread -Wl,--no-as-needed -std=c++11 -c /home/julien/Documents/test/main.cpp -o obj/Debug/main.o g++ -o bin/Debug/test obj/Debug/main.o ``` I still have the exact same error. I really don't know what to try. Do you have any idea ?

Original source

Related problems