一、使用gcc进行程序编译
(1)对简单的hello world程序进行编译并运行
1、 在ubuntu系统中使用vim编写hello.c文件
2、使用gcc对hello.c进行编译,并运行编译好的程序

其中gcc -o 可将.c文直接编译为可执行文件
(2)对一个需要复杂的程序进行编译并运行
1、编写main1.c
#include"stdio.h"
#include"sub1.c"
void main ()
{
int a = 2;
int b = 4;
float c = x2x(a,b);
printf("c = %.2f\n",c);
}
2、编写sub.c
float x2x(float a,float b)
{
float sum = 0;
sum = a+b;
return sum;
}
3、使用gcc对main1进行编译,并运行

(3)windows系统下main1.c进行编译
用以上两个文件在clion中运行,会报出x2x重定义的错误
![]()
需在mian1.c中声明加上 extern
#include"stdio.h"
extern float x2x(float a,float b);
void main ()
{
int a = 2;
int b = 4;
float c = x2x(a,b);
printf("c = %.2f\n",c);
}
float x2x(float m,float n)
{
float sum;
sum = m+n;
return sum;
}
可得结果

二、使用makefile进行程序编译
使用makefile对程序进行编程,也需要如windows系统一样,得避免重定义的错误,故更改程序如上方windows中的代码。
再vim一个makefile文档,内容如下:

再在终端中使用make命令编译程序,再运行

C语言编译实践:gcc与Makefile避免重定义错误,
本文介绍了如何在Ubuntu和Windows系统中使用gcc编译C语言程序,包括基本HelloWorld程序和复杂函数调用。特别提到在Windows下使用Makefile时处理函数重定义问题的方法。
502

被折叠的 条评论
为什么被折叠?



