不同方式不同系统编译main1.c程序

不同方式不同系统编译main1.c程序


问题描述

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

一、gcc编译hello.c

代码如下:
#include<stdio.h>
int main()
{
	printf("hello world!");//输出hello world
	return 0;
}

效果展示:
在这里插入图片描述
说明:
gcc的编译流程

  1. 预处理,生成预编译文件
    gcc -E hello.c -o hello.i
  2. 编译,生成汇编代码
    gcc -S hello.i -o hello.s
  3. 汇编,生成目标文件
    gcc -c hello.s -o hello.o
  4. 链接,生成可执行文件
    gcc hello.o -o hello

二、不同系统编译main.c

//main.c
#include<stdio.h>
float x2x(int a,int b);
void main()
{
	int a,b;
	printf("Please input the value of a:");
	scanf("%d",&a);
	printf("Please input the value of b:");
	scanf("%d",&b);
	printf("%.2f\n",x2x(a,b));
}
//sub1.c
float x2x(int a,int b)
{
	float c=0;
	c=a+b;
	return c;
}
  1. gcc编译main.c
    结果展示:
    在这里插入图片描述

  2. windows下Visual Studio 2019编译main.c
    在这里插入图片描述

三、Makefile方式编译main.c

Makefile文件内容:

CC=gcc
objects=scr/main.o scr/sub1.o
target=main

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

scr/main.o:scr/main.c
	$(CC) -o $@ -c $<

scr/sub1.o:scr/sub1.c
	$(CC) -o $@ -c $<

clean:
	rm -rf $(objects) $(target)

结果展示:
在这里插入图片描述

四、总结

提示:仅供参考
在Ubuntu中gcc与Make file编译的方式,gcc更适合比较少的源代码的编译,而对于Make file编译来说,它针对较大的项目(含有多个源程序)更有优势,更加方便。在Windows下,编译主要是在软件中直接完成。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值