在Linux环境下运行gcc
(GNU Compiler Collection)来编译C语言程序是一个常见的任务。以下是详细步骤和相关概念:
hello.c
:hello.c
:gcc
命令编译源文件:gcc
命令编译源文件:gcc
:调用GCC编译器。-o hello
:指定输出文件名为hello
。hello.c
:源代码文件。hello
,运行它:hello
,运行它:gcc: command not found
,说明GCC未安装或不在环境变量中。可以通过包管理器安装,如上文所述。-l
选项指定需要的库,例如:-l
选项指定需要的库,例如:假设我们有一个稍微复杂的C程序example.c
,使用了数学库中的函数:
#include <stdio.h>
#include <math.h>
int main() {
double num = 16.0;
printf("The square root of %.2f is %.2f\n", num, sqrt(num));
return 0;
}
编译并运行这个程序:
gcc -o example example.c -lm
./example
输出将会是:
The square root of 16.00 is 4.00
通过以上步骤,你应该能够在Linux环境下顺利使用GCC进行C语言程序的编译和运行。
领取专属 10元无门槛券
手把手带您无忧上云