在 Ubuntu 上“配置 C++ 失败”是一个比较笼统的说法,常见原因通常集中在编译器未安装、环境变量/路径问题、构建工具配置错误、权限或依赖缺失等方面。下面我按最常见场景帮你系统性梳理,并给出对应解决方法。
g++ 或 gcc 提示:Command 'g++' not found
sudo apt update
sudo apt install -y build-essential
验证:
g++ --version
gcc --version
CMake Error: C++ compiler not foundThe CXX compiler identification failedsudo apt install cmake
export CC=gcc
export CXX=g++
Ctrl+Shift+B 编译失败tasks.json / launch.jsong++: command not foundtasks.json:{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["main.cpp", "-o", "main"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
fatal error: iostream: No such file or directory
或
undefined reference to ...
gcc 编译 C++ 文件(错误)g++ main.cpp -o main
sudo apt install libssl-dev
Permission deniedchmod +x main
./main
apt install 失败sudo apt update
sudo apt upgrade
如果你愿意,可以贴出:
lsb_release -a)我可以直接告诉你是哪一步配置错了,以及具体改法。