VS2019与Ubuntu系统使用gcc和Makefile编译c程序
目录
编写一个主程序文件 main1.c 和一个子程序文件 sub1.c,子程序sub1.c 包含一个算术运算函数 float x2x(int a,int b),此函数功能为对两个输入整型参数做乘法运算,将结果做浮点数返回;主程序main1.c,定义并赋值两整型变量,然后调用函数 x2x,将x2x的返回结果printf出来。在ubuntu系统用gcc 命令行方式编译主程序main1.c,在windows系统下用VS2019编译主程序main1.c,在ubuntu系统下用Makefile方式编程主程序。
(一)使用gcc编译c
1.编写源程序
编写main1.c
#include<stdio.h>
#include"sub1.h"
int main(void)
{
int a, b;
scanf_s("%d%d", &a,&b);
printf("%.2f\n", x2x(a, b));
return 0;
}
编写sub1.h
#include <stdio.h>
float x2x(int a, int b);
编写sub1.c
#include<stdio.h>
float x2x(int a,int b)
{
float c;
c = (float)a * b;
return c;
}
2.gcc 命令行方式编译
使用scanf_s提示错误,修改为scanf
使用gcc -c sub1.c将sub1.c程序转换为目标文件sub1.o
使用gcc main1.c sub1.o -o main1将main1.c转换为目标文件main1.o,链接sub1.o目标文件生成main1可执行文件
使用./main1执行生成的main1文件
(二)使用VS2019编译c
编译并进行调试
(三)使用Makefile编译c
1.makefile使用格式
target:prerequisites
command # command以一个tab键开始
#target为生成的目标文件
#prerequisites是生成target所需要的文件
#command为make所执行的命令