在 CentOS 上写并运行 C++ 程序(不是严格意义上的“脚本”,但可以被当作脚本来用),一般步骤如下:
g++ --version
如果没有安装:
sudo yum install gcc-c++
#include <iostream>
int main() {
std::cout << "Hello CentOS" << std::endl;
return 0;
}
g++ hello.cpp -o hello
./hello
输出:
Hello CentOS
#!/usr/bin/g++ -o /tmp/cpp_tmp && /tmp/cpp_tmp
#include <iostream>
int main(){ std::cout << "hi" << std::endl; }
然后:
chmod +x hello.cpp
./hello.cpp
⚠️ 不推荐,编译慢,只是玩具用法。
#!/bin/bash
g++ -std=c++11 test.cpp -o /tmp/test && /tmp/test
g++ -std=c++11 test.cpp -O2 -Wall -o test
-std=c++11/14/17-O2 优化-Wall 显示警告g++ main.cpp func.cpp -o app
如果你其实想要 像 Python 一样直接运行的脚本,可以考虑:
clingcpp.sh如果你愿意,可以告诉我:
我可以给你更合适的写法。