在 Debian 上配置 GCC 开发环境,一般包含安装 GCC、相关工具链、管理多版本、验证与简单示例几个步骤。下面按常见使用场景说明。
sudo apt update
sudo apt upgrade -y
sudo apt install -y gcc g++
如果你还需要 make、构建工具:
sudo apt install -y build-essential
build-essential会包含:
- gcc
- g++
- make
- libc 开发头文件
gcc --version
g++ --version
make --version
看到版本号即说明环境配置成功。
// hello.c
#include <stdio.h>
int main() {
printf("Hello Debian GCC\n");
return 0;
}
编译并运行:
gcc hello.c -o hello
./hello
// hello.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello Debian C++" << endl;
return 0;
}
编译并运行:
g++ hello.cpp -o hello_cpp
./hello_cpp
Debian 通常提供多个 GCC 版本,例如 gcc-9、gcc-11、gcc-12。
apt-cache search gcc | grep '^gcc-'
sudo apt install -y gcc-11 g++-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 110
切换版本:
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
sudo apt install -y gdb
sudo apt install -y valgrind
sudo apt install -y cmake
sudo apt install -y libssl-dev
gcc main.c -o app \
-O2 \ # 优化
-Wall \ # 开启警告
-g \ # 生成调试信息
-std=c11 # C 标准
C++:
g++ main.cpp -o app -O2 -Wall -g -std=c++17
如果你有具体需求(比如:
可以告诉我,我可以给你更针对性的配置方案。