Setting up eclipse with C ++ 11

First of all, I have already tested different solutions:

Std :: system_error threads

Std :: system_error II topics

Compile multithreading with gcc

Gcc multithreading error

My environment:

Ubuntu 1404 64 bit

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 was able to compile and run a basic threading program (the code was taken from one of the links in SO but can't find it again. If anyone sees it, edit my question to add a link).

#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 as follows ( 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 I am trying to set up an eclipse project. I can compile, but it cannot create a valid output file.

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 builder, dialects ... as the links say, trying to get the same command that I can use to compile from the terminal. But there is no way to create a valid output file from 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 for customizing eclipse?

Update: Following @Dirk's directions , I tested it on a VM and it just adds pthread to the linker libraries.

But for my initial setup, it still doesn't work. I ran it after changing the c ++ c ++ linker settings for g ++ linker ing++ --std=c++0x -pthread

So it seems clear that something is missing in my first environment.

+3


source to share


1 answer


It looks like there is no reference to the pthread library. 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

      

Then your code works and produces:



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

+1


source







All Articles