c++ - 使用 C++11 配置 Eclipse

标签 c++ eclipse multithreading c++11

首先,我已经检查了不同的解决方案:

Threads std::system_error

Threads std::system_error II

Compile multithreading with gcc

Bug gcc multithreading

我的环境:

Ubuntu 1404 64 位

海湾合作委员会 4.8.2

面向 C/C++ 开发人员的 Eclipse IDE

版本:Luna Service Release 1 (4.4.1) 构建 ID:20140925-1800

按照这些链接,我设法编译并运行了一个基本的线程程序(代码取自 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;
}

我用来编译它的命令如下(THIS WORKS 并且输出文件是可执行的):

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

问题是当我尝试设置我的 eclipse 项目时。我可以编译,但无法生成有效的可运行输出。

编译日志:

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)

我正在为构建器、方言更改不同的设置……正如他们在链接中所说的那样,试图获得我可以用来从终端编译的相同命令。但是无法从 Eclipse 创建有效的输出文件。它总是显示此错误:

在抛出“std::system_error”实例后调用终止 what(): 启用多线程使用 std::thread: Operation not permitted

知道如何设置 Eclipse 吗?

更新: 按照@Dirk的指示我在虚拟机中进行了测试,它只需将 pthread 添加到链接器库即可工作。

但对于我的初始设置,它仍然失败。我在将 C/C++ build设置 G++ 链接器命令修改为 g++ --std=c++0x -pthread

后使其工作

所以,很明显我的第一个环境缺少一些东西。

最佳答案

似乎缺少与 pthread 库的链接。在 Build->Settings->Tool Settings->GCC C++ Linker->Libraries 下添加“pthread”。

这是我的构建日志所说的:

**** 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

然后您的代码可以运行并输出:

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

如果没有 -lpthread,我会得到和你一样的错误

我的链接器设置: linkersettings

关于c++ - 使用 C++11 配置 Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26673574/

相关文章:

c++ - 奇怪地使用枚举作为常量

c++ - 命名对象与临时对象 : Is it better to avoid named objects when possible?

Eclipse 帮助框

java - 在 Eclipse 中读取项目中的 .txt 文件

python - 提高Python脚本速度: Multithreading or Multiple Instances?

Java 线程 : Figure out which threads are still running

c++ - C/C++中数组中最小值的地址

c++ - 我需要在这个函数前加上 __stdcall 前缀吗?

java - 依赖二进制文件如何包含在 Maven 中最终构建和安装的 .JAR 中?

java - Java中的线程结构