关于Eclipse下启用并编译 C++ 11 的问题
最近在学习C++,在学习C++11的时候,发现eclipse不能支持C++11的编译,经过一番折腾,终于将其搞定,下面附上操作方法,谨防忘记…
我的环境是 Ubuntu 14.0 64-bit 使用 GNU 的 G++ 4.9.1 编译器,IDE是 Eclipse IDE for C/C++ Developers ( Luna Service Release 1a (4.4.1) )
- 首先在eclipse中创建一个新项目,右击项目名,选择 Properties(Alt + Enter)
- 在 C/C++ Build 中选择 Setting ,找到右侧的 GCC C++ Compiler 下的 Miscellaneous
- 在 Other flags 里最后加入 -std=c++11 ,这样就完成了编译 C++ 11 的选项
- 然后在 C/C++ Build 中选择 Preprocessor
- 在 Defined symbols 中加入 __cplusplus=201103L
这样就大功告成啦
测试代码如下:
/*
* main.cpp
*
* Created on: Feb 8, 2015
* Author: dlll
*/
#include <iostream>
#include <vector>
#include <thread>
int main() {
std::vector threads;
for (int i = 0; i < 5; ++i) {
threads.push_back(std::thread([]() {
std::cout << "Hello from lamda thread " << std::this_thread::get_id() << std::endl;
}));
}
for (auto& thread : threads) {
thread.join();
}
std::cout << "Main Thread" << "\t" << std::this_thread::get_id() << std::endl;
return 0;
}
在编译这段代码时,遇到一个错误
/usr/include/c++/4.9/thread:136: undefined reference to `pthread_create’
这是由于没有添加pthread这个库文件导致的
添加方法同上,在 GCC C++ Linker 中的 Libraries 添加 pthread 即可



近期评论