简体   繁体   English

使用c ++ 11配置eclipse

[英]Configure eclipse with c++11

First of all, I have already checked different solutions: 首先,我已经检查了不同的解决方案:

Threads std::system_error 线程std :: system_error

Threads std::system_error II 线程std :: system_error II

Compile multithreading with gcc 用gcc编译多线程

Bug gcc multithreading Bug gcc多线程

My environment: 我的环境:

Ubuntu 1404 64bits Ubuntu 1404 64位

gcc 4.8.2 gcc 4.8.2

Eclipse IDE for C/C++ Developers 用于C / C ++开发人员的Eclipse IDE

Version: Luna Service Release 1 (4.4.1) Build id: 20140925-1800 版本: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). 按照这些链接,我设法编译并运行一个基本的线程程序(代码来自SO中的一个链接,但无法再找到它。如果有人看到它,请编辑我的问题以添加引用)。

#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. 问题是当我尝试设置我的eclipse项目时。 I can compile but it cannot produce a valid runnable output. 我可以编译,但它不能产生有效的runnable输出。

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. 但无法从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? 知道如何设置eclipse吗?

Update: Following the indications of @Dirk I tested in a virtual machine and it works just adding the pthread to the linker libraries. 更新:遵循@Dirk的指示我在虚拟机中进行了测试,它只是将pthread添加到链接器库。

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 在将C / C ++ Build设置G ++链接器命令修改为g++ --std=c++0x -pthread后,我使其工作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. 似乎缺少与pthread库的链接。 Add "pthread" under Build->Settings->Tool Settings->GCC C++ Linker->Libraries. 在Build-> Settings-> Tool Settings-> GCC C ++ Linker-> Libraries下添加“pthread”。

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 我得到了与没有-lpthread相同的错误

My linker settings: 我的链接器设置: linkersettings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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