3

First of all, I have already checked different solutions:

Threads std::system_error

Threads std::system_error II

Compile multithreading with gcc

Bug gcc multithreading

My environment:

Ubuntu 1404 64bits

gcc 4.8.2

Eclipse IDE for C/C++ Developers

Version: Luna Service Release 1 (4.4.1) Build id: 20140925-1800

Following these links I managed to compile and run a basic thread program(the code was taken from one of the links in SO, but cannot find it again. If someone sees it, please edit my question to add the reference).

#include <iostream>
#include <thread>
#include <vector>

void func(int tid)
{
    std::cout << "Launched by thread " << tid << std::endl;
}

int main()
{
    std::vector<std::thread> th;

    int nr_threads = 10;
    for(int i = 0; i < nr_threads; ++i)
    {
        th.push_back(std::thread(func, i));
    }

    for(auto &t : th)
    {
        t.join();
    }

    return 0;
}

The command I use to compile it is the following(THIS WORKS and the output file is executable):

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

Launched by thread 1
Launched by thread 5
Launched by thread 3
Launched by thread 6
Launched by thread 4
Launched by thread 0
Launched by thread 7
Launched by thread 8
Launched by thread 9
Launched by thread 2

The problem is when I try to setup my eclipse project. I can compile but it cannot produce a valid runnable output.

Compile log:

12:09:45 **** Incremental Build of configuration Debug for project test ****
make all 
Building file: ../test.cpp
Invoking: GCC C++ Compiler
g++ --std=c++11 -pthread -D__cplusplus=201103L -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"test.d" -MT"test.d" -o "test.o" "../test.cpp"
Finished building: ../test.cpp

Building target: test
Invoking: GCC C++ Linker
g++  -o "test"  ./test.o   
Finished building target: test


12:09:46 Build Finished (took 780ms)

I was changing different settings for the builder, dialects... as they say in the links trying to get the same command I can use to compile from the terminal. But no way to create a valid output file from the eclipse. It always shows this error:

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

Any idea how to setup eclipse?

Update: Following the indications of @Dirk I tested in a virtual machine and it works just adding the pthread to the linker libraries.

But for my initial setup it still fails. I made it working after modifying the C/C++ Build settings G++ linker command to g++ --std=c++0x -pthread

So, it seems obvious that my first environment is missing something.

Community
  • 1
  • 1
blfuentes
  • 2,731
  • 5
  • 44
  • 72
  • How do you perform the linking in your working example? Looks like eclipse is configured to use -pthread for compiling but not for linkage. – Dirk Oct 31 '14 at 11:30
  • I didn't change anything in the g++ linker settings. When I added the flag -pthread it throws `make: *** No rule to make target -pthread', needed by test'. Stop.` when building – blfuentes Oct 31 '14 at 11:33
  • Simply add pthread under the library options of the linker settings. – Dirk Oct 31 '14 at 11:44
  • I added it and the linker output now is different g++ -o "test" ./test.o -lpthread` But still same error on running it – blfuentes Oct 31 '14 at 11:48
  • Hmm... What is your output? The ` in your comment is not from copy/paste? – Dirk Oct 31 '14 at 13:51
  • the ` was a typo error in my comment now trying to do the grey-style `comment` – blfuentes Oct 31 '14 at 13:53

1 Answers1

1

Seems like linking with the pthread library is missing. Add "pthread" under Build->Settings->Tool Settings->GCC C++ Linker->Libraries.

This is what my build log says:

**** Build of configuration Debug for project testpthreads ****

make all 
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++0x -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp

Building target: testpthreads
Invoking: GCC C++ Linker
g++  -o "testpthreads"  ./main.o   -lpthread
Finished building target: testpthreads

Your code then works and outputs:

Launched by thread Launched by thread Launched by thread 1
Launched by thread 0
Launched by thread 2
Launched by thread 9
Launched by thread 3
Launched by thread 7
Launched by thread 6
4
Launched by thread 8
5

I get the same error as you Without -lpthread

My linker settings: linkersettings

Dirk
  • 1,789
  • 2
  • 21
  • 31