95

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 the correct link options to use std::thread in GCC under linux?

PPPPPS:

$ dpkg --get-selections | grep 'libc.*dev'
libc-dev-bin                    install
libc6-dev:amd64                 install
libclang-common-dev             install
linux-libc-dev:amd64                install
Community
  • 1
  • 1
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • It's a symlink to `libpthread-2.17.so` in the same directory. – zerkms Oct 19 '13 at 09:43
  • Yes, that is a correct link. Here is my dir lrwxrwxrwx. 1 root root 18 Sep 12 20:52 libpthread.so.0 -> libpthread-2.16.so – Jason Enochs Oct 19 '13 at 09:43
  • @Jason Enochs: it would work for me - I agree, if I didn't use buggy gcc version – zerkms Oct 19 '13 at 09:50
  • Yea, all of your libraries are a bit newer than mine: gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC) – Jason Enochs Oct 19 '13 at 09:53
  • @Jason Enochs: hehe. For myself I'm already satisfied with "it's a bug" answer, and probably will try to run it on an older ubuntu tomorrow, just to be completely sure – zerkms Oct 19 '13 at 10:30

4 Answers4

75

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

It looks like this behaviour is caused by a bug in gcc.

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

-Wl,--no-as-needed
Community
  • 1
  • 1
zerkms
  • 249,484
  • 69
  • 436
  • 539
39

Adding -lpthread fixed the identical problem for me:

 g++ -std=c++11 foo.cpp -lpthread -o foo
piet.t
  • 11,718
  • 21
  • 43
  • 52
Petr Vepřek
  • 1,547
  • 2
  • 24
  • 35
  • 1
    Jep, this is the anwser and also completely reasonable. "-Wl,--no-as-needed" sounded weird and also doesn't work for me. – thesaint Mar 16 '15 at 09:44
  • 3
    You definitely need `-lpthread`. You *might* need `-pthread -Wl,--no-as-needed`, depending on compiler version. **gcc-4.8.2** needs it. – cdunn2001 Mar 23 '16 at 18:26
15

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".

user31264
  • 6,557
  • 3
  • 26
  • 40
  • Well, the solutions were provided for the different version and I can assure the checked answer did work at the moment I checked it. Anyway, thanks for the actual info. – zerkms May 09 '16 at 22:01
10

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
Community
  • 1
  • 1
ecoretchi
  • 127
  • 1
  • 3
  • 1
    Thank you, your answer is the only one that makes it clear that the -pthread has to be passed to the compiler and the linker. – denim Mar 02 '15 at 21:04