简体   繁体   中英

Configure eclipse with c++11

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM