在Linux环境下,可以使用编译器将C语言源代码编译成可执行文件。常用的C语言编译器有GCC和Clang。
1. 安装编译器:
- 如果使用GCC编译器,在终端中运行命令 `sudo apt-get install gcc` 或 `sudo apt-get install build-essential`。
- 如果使用Clang编译器,在终端中运行命令 `sudo apt-get install clang`。
2. 编写C语言源代码:
使用任何文本编辑器,创建一个以`.c`为后缀的C语言源代码文件,例如`hello.c`,并在其中编写C语言程序。例如:
```c
#include
int main() {
printf("Hello World!\n");
return 0;
}
```
3. 编译C语言源代码:
打开终端,进入源代码所在目录,并运行以下命令:
- 使用GCC编译器:`gcc hello.c -o hello`
- 使用Clang编译器:`clang hello.c -o hello`
上述命令将会将`hello.c`源代码文件编译为可执行文件`hello`。
4. 运行可执行文件:
在终端中运行命令 `./hello`,即可执行刚才编译得到的可执行文件,输出将会是 `Hello World!`。