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

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

文章目录

  • 不同方式不同系统编译main1.c程序
    • 问题描述
    • 一、gcc编译hello.c
    • 二、不同系统编译main.c
    • 三、Makefile方式编译main.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
int main()
{
     
	printf("hello world!");//输出hello world
	return 0;
}

效果展示:
不同方式不同系统编译main1.c程序_第1张图片
说明:
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
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
    结果展示:
    不同方式不同系统编译main1.c程序_第2张图片

  2. windows下Visual Studio 2019编译main.c
    不同方式不同系统编译main1.c程序_第3张图片

三、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)

结果展示:
不同方式不同系统编译main1.c程序_第4张图片

四、总结

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

你可能感兴趣的:(不同方式不同系统编译main1.c程序)