目录
(一)用gcc命令行方式编译主程序
1.编写c程序
•编写主程序文件main.c
#include "sub1.h"
int main()
{
int x=3,y=4; //定义a,b变量
printf("%.2f\n",x2x(x,y)); //保留两位小数输出结果
return 0;
}
•编写sub1.h
#ifndef _sub1_h
#define _sub1_h
#include<stdio.h>
#include<math.h>
float x2x(int a, int b);
#endif
•编写子程序sub1.c
#include "sub1.h"
float x2x(int a,int b)
{
float ans; //返回值
ans=float(a/b);
return ans;
}
2.通过gcc编译
•将sub1.c程序转换为目标文件sub1.o:gcc -c sub1.c
•编译main1.c文件为目标文件main1.o,然后将sub1.o目标文件转换为可执行文件:gcc main1.c sub1.o -o main1
•编译生成的main1程序:./main1
3.用Windows编译工具编译main.c并进行对比
工具:Visual Studio 2019
(二) 用Makefile方式编译主程序
1.编写Makefile文件
Makefile文件编写格式:目标文件(target):依赖文件(prerequiries)...执行命令(command)
main1:sub1.o main1.c
gcc main1.c sub1.o -o main1
sub1.o:sub1.c
gcc -c sub1.c -o sub1.o
clean: rm *.o //清空(伪)目标文件
2.使用Makefile文件编译c程序