在 Ubuntu 上配置 C++ 开发环境通常包含以下几个步骤:安装编译器、配置构建工具、选择编辑器/IDE、以及(可选)安装调试工具。下面按从简单到完整的方式说明。
sudo apt update
sudo apt install build-essential
build-essential 包含:
gcc(C 编译器)g++(C++ 编译器)make(构建工具)g++ --version
若能看到版本号,说明安装成功。
mkdir ~/cpp_demo && cd ~/cpp_demo
nano hello.cpp
写入内容:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, Ubuntu C++!" << endl;
return 0;
}
g++ hello.cpp -o hello
./hello
创建 Makefile:
hello: hello.cpp
g++ hello.cpp -o hello
clean:
rm -f hello
使用:
make
./hello
make clean
sudo apt install gdb
编译时加 -g:
g++ -g hello.cpp -o hello
gdb ./hello
sudo apt install cmake
示例 CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(hello)
add_executable(hello hello.cpp)
使用:
mkdir build && cd build
cmake ..
make
./hello
sudo snap install code --classic
安装插件:
sudo apt install qtcreator
sudo apt install gcc-11 g++-11
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100
sudo apt install libboost-all-dev
sudo apt install libopencv-dev
如果你愿意,可以告诉我:
我可以给你一套更针对性的配置方案。