4

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 -pthread -std=c++0x in different orders also, but I still get the:

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

What to do?

Info: 
Ubuntu 12.04LTS
Running Eclipse CDT
g++ v4.8.1 

Edit: I tried building with -Wl,--no-as-needed -lpthread -std=c++0x with no luck. The code:

#include <iostream>
#include <chrono>
#include <thread>

void foo()
{
    std::cout << "Thread 1 created.." << std::endl;
}

int main()
{
    std::thread t1(foo);

    t1.join();

    return 0;
}

Edit: So unfortunately none of your suggestions worked. I decided to use Boost instead.

Community
  • 1
  • 1
user3452622
  • 41
  • 1
  • 4

2 Answers2

4
  1. it's -Wl,--no-as-needed not -Wl,--no_as_needed, you use the hyphen
  2. -pthread is a flag for the compiler, not the linker, the right one for the linker is -lpthread
  3. Mingw doesn't always comes with the same threading library, there are more than 1 options for multithreading with MinGW, you should document yourself about this according to your MinGW build
user2485710
  • 9,451
  • 13
  • 58
  • 102
  • 1
    Your (1) and (3) are correct, but the compiler (technically, the "driver" program that is installed as `/usr/bin/gcc`; `-v` will show you what it does) converts `-pthread` to `-lpthread` when it is asked to perform a link. – zwol Mar 23 '14 at 16:43
  • @Zack yes, in other words the `-pthread` flag alone should be enough, but in my experience it's not, I have encountered situations where `pthread` doesn't activate `lthread`, It was probably due to some customization for the given `gcc` build that I was using, but sometimes I have found the `-lpthread` flag necessary even with `pthread`. – user2485710 Mar 23 '14 at 16:45
  • 1. Typo mistake. 2. I tried with `-Wl,--no-as-needed -lpthread -std=c++0x` and had no luck. Still same error message. 3. My toolchain is set to "Linux GCC", so I don't think I'm using MinGW? – user3452622 Mar 23 '14 at 16:51
  • @user2485710 Still same error with `-std=c++0x -pthread`. Or `-std=c++0x -lpthread` (tried both).. Hmm. – user3452622 Mar 23 '14 at 17:03
1
g++  filename.c -std=c++11 -lpthread

i am compiling your code with above command its working perfect.

nKn
  • 13,691
  • 9
  • 45
  • 62
Vinod Patidar
  • 372
  • 1
  • 4
  • 12