Ubuntu 下 C++ 配置“失败”的原因非常多,常见情况可以归为下面几类。你可以对照看看自己是哪一种,我再可以帮你具体排查。
很多人以为装了 Ubuntu 就能直接编译 C++,其实默认可能没装。
检查:
g++ --version
如果提示 command not found,说明没装。
解决:
sudo apt update
sudo apt install build-essential
错误示例:
gcc main.c // 实际是 C++ 文件
或
g++ main.cpp -o main
但文件里写了 C++ 特性却用了 .c 后缀。
✅ 建议:
.cpp / .cc / .cxxg++典型错误:
fatal error: iostream: No such file or directory
原因:
g++解决:
sudo apt install g++
如果你是在 Ubuntu + VSCode 下配置 C++ 失败:
常见错误:
tasks.json 路径错launch.json 没配 debugger最小可用配置示例:
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g", "main.cpp", "-o", "main"]
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main",
"preLaunchTask": "build"
}
]
}
如果你用 CMake:
常见错误:
检查:
cmake --version
最小 CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(demo)
add_executable(demo main.cpp)
Permission denied
解决:
chmod +x main
lsb_release -a)g++如果你愿意,把报错截图或文字贴出来,我可以直接帮你改到能跑 ✅