分别在ubuntu下用gcc,Makefile和Window下用编译工具编译main.c程序

一.实验要求

1、请编写一个主程序文件 main1.c 和一个子程序文件 sub1.c, 要求:子程序sub1.c 包含一个算术运算函数 float x2x(int a,int b),此函数功能为对两个输入整型参数做某个运算,将结果做浮点数返回;主程序main1.c,定义并赋值两整型变量,然后调用函数 x2x,将x2x的返回结果printf出来。
2、请在ubuntu系统用gcc 命令行方式编译主程序main1.c 并运行;
3、 请在windows系统下用你熟的编译工具编译主程序main1.c 并运行。
4、在上述任务基础上,在ubuntu系统下用Makefile方式编程主程序。

二.实验过程

1.使用命令gedit main1.c编辑主程序文件

#include<stdio.h>

float x2x(int a,int b);

int main()

{

  int a;

  int b;

  printf("please input a:");

  scanf("%d",&a);

  printf("please input b:");

  scanf("%d",&b);

  printf("%.2f",x2x(a,b));

  return 0;

}

2.使用gedit sub1.c编辑子程序文件

#include<studio.h>

float x2x(int a,int b)

{

  float c;

  c=a+b;

  return c;

}

3.使用gcc main1.c sub1.c命令运行程序
在这里插入图片描述

4.在Windows系统下用Qt编译mian程序并运行
在这里插入图片描述

5.用Makefile方式编程主程序

使用命令gedit Makefile编辑文件

CC=gcc

objects=a/main1.o a/sub1.o

target=main1

 

$(target):$(objects)

​    $(CC) -o $(target) $(objects)

 

a/main1.o:a/main1.c

​    $(CC) -o $@ -c $<

 

a/sub1.o:a/sub1.c

​    $(CC) -o $@ -c $<

运行
在这里插入图片描述

三.总结

通过本实验的学习,深刻体会使用makefile方式编辑主程序的方便之处。

四.参考链接

https://blog.csdn.net/qq_45237293/article/details/108741893
https://www.cnblogs.com/zlee-home/p/13708004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值