您的位置:首页 > 编程语言 > C语言/C++

如何让你的Sublime和Codeblocks支持C++11 (转载)

2014-01-11 21:50 369 查看
转自:http://www.360doc.com/content/13/0913/11/9200790_314152981.shtml

闲来没事看了一下C++11,比起C++0x多了很多新功能,像auto变量,智能指针等,g++4.7以上版本也提供了对C++11的支持,但是,如何在你的编辑器上执行C++11代码呢?

刚开始以为用法和以前的版本一样,于是写了个C++11的小代码:



完事后一编译发现不对,于是又手工调用g++编译了一下:

g++ -o test2 test2.cpp


发现还是不对。

百度了一下才发现原来编译C++11不同于C++0x,要加一个编译选项-std=c++11 :

g++ -std=c++11 -o test2 test2.cpp


编译顺利通过!!!

可是,如何将这个编译选项应用到IDE上呢?

我常用的编辑器是Codeblocks和Sublime

找了一下,其实Codeblocks的设置蛮简单的:

Setting->Compiler

直接在“Have g++ follow the C++11 ISO C++ language standard [-std=c++11]” 选项上打勾 保存就可以了



Sunlime的配置则比较麻烦一些:

Tools->Build System->New Build System...

然后把下面的代码粘贴到新打开的文件中:

1 {
2      "cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"], // For GCC On Windows and Linux
3      //"cmd": ["CL", "/Fo${file_base_name}", "/O2", "${file}"],     // For CL on Windows Only
4      "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
5      "working_dir": "${file_path}",
6      "selector": "source.c, source.c++",
7
8      "variants":
9      [
10           {
11                "name": "Run",
12                //"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]  // Linux Only
13                "cmd": ["CMD", "/U", "/C", "g++ -std=c++11 ${file} -o ${file_base_name} && ${file_base_name}"]  // For GCC On Windows Only
14                //"cmd": ["CMD", "/U", "/C", "CL /Fo${file_base_name} /O2 ${file} && ${file_base_name}"]   // For CL On Windows Only
15           }
16      ]
17 }


然后保存,在保存文件对话框中把文件名字改成“C++11.sublime-build” 保存即可

然后选择Tools->Build System->C++11

这时你的Sublime Text就是一个完美的支持C++11的编辑器了!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: