打开终端, 依次进行以下操作
- 新建hello.c文件,并写入代码
> alisa@localhost ~/c/c> touch hello.c
> alisa@localhost ~/c/c> vim hello.c
输入代码
#include <stdio.h>
int main()
{
/* 我的第一个 C 程序 */
printf("Hello, World! \n");
return 0;
}
- 通过gcc编译,如果没有错误,命令提示符会跳到下一行,同时会生成一个a.out可执行文件
alisa@localhost ~/c/c> ls -al
total 8
drwxr-xr-x 3 alisa staff 96 11 26 16:01 .
drwxr-xr-x 7 alisa staff 224 11 26 15:56 ..
-rw-r--r-- 1 alisa staff 122 11 26 16:01 hello.c
alisa@localhost ~/c/c> gcc hello.c
alisa@localhost ~/c/c> ls -al
total 40
drwxr-xr-x 4 alisa staff 128 11 26 16:02 .
drwxr-xr-x 7 alisa staff 224 11 26 15:56 ..
-rwxr-xr-x 1 alisa staff 12556 11 26 16:02 a.out
-rw-r--r-- 1 alisa staff 122 11 26 16:01 hello.c
- 最后,运行a.out文件,进行输出
>alisa@localhost ~/c/c> ./a.out
Hello, World!
请确保您的路径中已包含 gcc 编译器,并确保在包含源文件 hello.c 的目录中运行它。
注:如果是多个 c 代码的源码文件(test1.c 与 test2.c 是两个源代码文件。),编译方法如下:
$ gcc test1.c test2.c -o main.out
$ ./main.out