安裝 GCC 4.8(支援 C++11 的最低版本)
Ubuntu 12.04 預設倉庫 GCC 版本較舊,需加入 Toolchain PPA:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.8
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
g++ --version # 驗證版本
配置 Eclipse 全域/專案編譯參數
- 選單 Project → Properties → C/C++ Build → Settings → Miscellaneous
- 在 Other flags 加入
-std=c++11
- 若僅針對單一專案,右鍵專案 → Properties 同樣路徑設定
配置 CDT Indexer 正確解析 C++11 語法
此步驟讓編輯器正確識別 auto、lambda、nullptr 等關鍵字:
- Project → Properties → C/C++ General → Preprocessor Include Paths, Macros etc.
- 切換到 Providers 標籤
- Configuration 選 Debug(Release 同理再做一次)
- 勾選 CDT GCC Built-in Compiler Settings
- 取消勾選 Use global provider shared between projects
- 在 Command to get compiler specs 後追加
-std=c++11
- Apply → OK
- 最後 Project → C/C++ Index → Rebuild 重建索引
參考資源