[英]Configure eclipse with c++11
首先,我已经检查了不同的解决方案:
我的环境:
Ubuntu 1404 64位
gcc 4.8.2
用于C / C ++开发人员的Eclipse IDE
版本:Luna Service Release 1(4.4.1)Build 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;
}
我用来编译它的命令如下( 这个工作和输出文件是可执行的):
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项目时。 我可以编译,但它不能产生有效的runnable输出。
编译日志:
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创建有效的输出文件。 它总是显示此错误:
terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted
知道如何设置eclipse吗?
更新:遵循@Dirk的指示我在虚拟机中进行了测试,它只是将pthread添加到链接器库。
但对于我的初始设置,它仍然失败。 在将C / C ++ Build设置G ++链接器命令修改为g++ --std=c++0x -pthread
后,我使其工作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相同的错误
我的链接器设置:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.