下面以 Ubuntu + GCC 为例,教你写并运行你的第一个 C 程序(最常见入门示例)。
打开终端(Ctrl + Alt + T),输入:
gcc --version
如果看到类似 gcc (Ubuntu ...),说明已安装。
如果没安装,执行:
sudo apt update
sudo apt install gcc
mkdir hello
cd hello
nano hello.c
你也可以用
gedit hello.c或vim hello.c
#include <stdio.h>
int main() {
printf("Hello, Ubuntu!\n");
return 0;
}
按 Ctrl + O 保存,Ctrl + X 退出(nano)。
gcc hello.c -o hello
hello.c:源文件-o hello:生成可执行文件名为 hello./hello
输出:
Hello, Ubuntu!
如果提示 Permission denied:
chmod +x hello
./hello
gcc main.c func.c -o app
如果是 C++:
sudo apt install g++
#include <iostream>
using namespace std;
int main() {
cout << "Hello Ubuntu" << endl;
}
编译:
g++ hello.cpp -o hello
./hello
如果你愿意,我也可以教你:
你想从哪一步继续?