閑來沒事看了一下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的編輯器了!

Chierush原創,轉載請注明地址:http://www.cnblogs.com/Chierush/
