在Ubuntu上配置C++環境步驟如下:
安裝基礎開發工具
sudo apt update
sudo apt install build-essential # 包含GCC、G++、make等
(可選)安裝特定版本GCC:
sudo apt install gcc-11 g++-11
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11 # 切換版本
安裝IDE或編輯器
sudo snap install --classic code
安裝后需在擴展市場添加“C/C++”擴展包,并配置tasks.json
和launch.json
。sudo apt install codeblocks
安裝調試工具(可選)
sudo apt install gdb
測試環境
創建hello.cpp
文件:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
編譯并運行:
g++ hello.cpp -o hello
./hello
若輸出“Hello, World!”,則配置成功。
說明:
build-essential
是基礎開發工具包,包含編譯器、構建工具等,適合大多數場景。update-alternatives
管理。