在 Ubuntu 上做 C++ 开发时,“配置出错”可能涉及编译器、构建工具、CMake、依赖库、环境变量等多个方面。下面我按常见错误类型 → 排查步骤 → 解决办法给你一个系统性的指南。
g++ --version
gcc --version
如果没有:
sudo apt update
sudo apt install build-essential
典型表现:
fatal error: xxx.h: No such file or directoryundefined reference to ...sudo apt install libxxx-dev
例如:
sudo apt install libopencv-dev
编译时加 -l 和 -L:
g++ main.cpp -o app -lopencv_core
典型错误:
CMake Error: Could NOT find XXXCMake 3.x requiredcmake --version
sudo apt install cmake
cmake -DCMAKE_PREFIX_PATH=/your/lib/path ..
Permission denied
解决:
chmod +x your_program
error while loading shared libraries: libxxx.so
查看缺失库:
ldd your_program
添加库路径:
export LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH
或:
sudo ldconfig
tasks.json / launch.jsonc_cpp_properties.json 中的 include pathbuild-essential 已安装-dev 包已安装请把 具体错误信息(复制终端输出)发给我,例如:
CMake Error at CMakeLists.txt:10
或者:
fatal error: Eigen/Dense: No such file or directory
我可以一步一步教你改 ✅