“Ubuntu 跑不了 C++”通常并不是 Ubuntu 本身不支持 C++,而是环境或用法的问题。下面按常见原因给你梳理一下。
Ubuntu 是基于 Linux 的,而 Linux 原生就支持 C / C++。
问题通常出在:
g++ --version
如果显示:
Command 'g++' not found
说明没装。
sudo apt update
sudo apt install g++ -y
test.cpp#include <iostream>
using namespace std;
int main() {
cout << "Hello Ubuntu" << endl;
return 0;
}
g++ test.cpp -o test
./test
⚠️ 很多人会直接:
test.cpp ❌
这样是跑不了的。
chmod +x test
./test
如果 .cpp 文件是从 Windows 拷贝过来的:
\r\n 问题可修复:
dos2unix test.cpp
如果你用:
但没装:
也可能“看起来跑不了”。
有些 C++ 程序:
在服务器版 Ubuntu 上可能跑不了。
undefined reference → 没链接库segmentation fault → 代码 bugPermission denied → 没执行权限Ubuntu 能跑 C++,只是你可能需要:
- 安装
g++- 正确编译
- 正确运行
- 修好代码或环境
如果你愿意,可以贴:
我可以直接帮你定位问题。