Compiling multithread code with g++

I have the easiest code ever:

#include <iostream>
#include <thread>

void worker()
{
    std::cout << "another thread";
}

int main()
{
    std::thread t(worker);
    std::cout << "main thread" << std::endl;
    t.join();
    return 0;
}

though I still cannot compile it with g++ to run.

More details:

$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Command to compile:

$ g++ main.cpp -o main.out -pthread -std=c++11

Running:

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

And now I’m in stuck. In every related thread over the internet it’s recommended to add -pthread while I have it already.

What am I doing wrong?

PS: It’s a brand new ubuntu 13.10 installation. Only g++ package was installed and minor things like chromium etc

PPS:

$ ldd ./a.out 
linux-vdso.so.1 => (0x00007fff29fc1000) 
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb85397d000) 
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb853767000) 
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb85339e000) 
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb85309a000) 
/lib64/ld-linux-x86-64.so.2 (0x00007fb853c96000)

PPPS: with clang++ (v3.2) it compiles and runs fine

PPPPS: guys, it’s not a duplicate of What is wrong with this use of std::thread?

PPPPPS:

$ dpkg --get-selections | grep 'libc.*dev'
libc-dev-bin                    install
libc6-dev:amd64                 install
libclang-common-dev             install
linux-libc-dev:amd64                install

Compiling multithread code with g++ (-Wl,–no-as-needed NOT working)

My problem is actually described here: Compiling multithread code with g++. But the answer regarding the work around by using -Wl,–no-as-needed is not working for me. I’ve added -Wl,–no-as-needed

Compiling C++ code with allegro 5 and g++

What flags do I need to add to g++ in order to compile code using allegro 5? I tried g++ allegro5test.cpp -o allegro5test `allegro-config –libs` but that is not working. I’m using ubuntu 11.04. I in

Compiling FreeRTOS with GCC, app code with G++

I’ve got a Stellaris Launchpad board with a Cortex M4F on it. I want to use FreeRTOS on the board with some of my own C++ code. FreeRTOS, however, is written in C so will not compile with G++; I’ve tr

Singleton multithread code in C++

I have a doubt related to Singleton and multithread programming in C++ Following you can see an example code of a Singleton class with a variable named shared. I create 1000 threads that modify (+1) t

minGW (with g++ 4.7.1) not compiling simple C++11 code (under WinVista)

I did my homework and searched for an answer here and on the net. The simple code below is not compiling: #include <thread> #include <iostream> void hello() { std::cout << Hello fro

Linux g++ compiling

I’m new to programming on linux, and compiling in the terminal. I have three files: sql.h #ifndef SQL_H #define SQL_H #include sqlite3.h #include <string> class sqlite{ private: sqlite3 *db; s

g++ compiling option

What are options for compile .cpp file into .s and .o without linking it. I used g++ -s -c but it only produced .o file. g++ -s with create undefined reference to main (because it a class implementati

G++ Compiling Error with .cpp

I am having issues with compiling my .cpp file. There is nothing wrong with the code, and I suck at C++ and g++, so sorry if I suck at this. Anyways, I am getting the error message: ‘Main.cpp -o Main

Transforming code to multithread version

I have the following problem: for example, I have 10 lists, each one has a link to some other lists, I would like to create a code to search elements in theses lists, I’ve already done this algorithm

Error in compiling C code with variable name new with g++

We are trying a modify some existing C projects i.e. we are trying to make some C++ function calls from the C code. We tried changing compiler from gcc to g++ but there were several compilation errors

Answers

The answer was provided by a kind member of SO C++ chat.

It looks like this behaviour is caused by a bug in gcc: https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1228201

The workaround provided in the last comment of that bug discussion does work and solves the issue:

-Wl,--no-as-needed

Adding ‘-lpthread’ fixed identical problem for me: “g++ -std=c++11 foo.cpp -lpthread -o foo”.

answer already was made for qtcreator:

LIBS += -pthread
QMAKE_CXXFLAGS += -pthread
QMAKE_CXXFLAGS += -std=c++11

for console g++: here

g++ -c main.cpp -pthread -std=c++11         // generate target object file
g++ main.o -o main.out -pthread -std=c++11  // link to target binary

I have slightly more advanced version (4.8.4 instead of 4.8.1), and I tested all three answers above. In fact:

-pthread alone works:

g++ -std=c++11 -o main -pthread main.cpp

-Wl,–no-as-needed alone does not work.

-lpthread alone does not work.

-Wl,–no-as-needed and -lpthread together work:

g++ -std=c++11 -o main -Wl,–no-as-needed main.cpp -lpthread

My version is “g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4”.