当前位置:网站首页>第一次使用gcc和makefile编写c程序
第一次使用gcc和makefile编写c程序
2022-06-28 01:03:00 【m0_61811389】
一、用gcc编写一个主函数main.c和子程序sub.c
1.编写一个主函数main.c
#include"stdio.h"
float x2x(int a,int b);
int main()
{
int x=2,y=4; //定义x和y变量
printf("%.2f\n",x2x(x,y));
return 0;
}2.编写一个子函数sub.c
float x2x(int a,int b)
{
float c;
c=a+b;
return c;
}3.运行程序
4.windows用dev c++编写
sub.h
#include"stdio.h"
float x2x(int a,int b)
{
float c;
c=a+b;
return c;
}main.c
#include"stdio.h"
#include"sub.h"
float x2x(int a,int b);
int main(){
int x=2,y=4;
printf("%.2f\n", x2x(x,y));
return 0;
}运行程序

二、用Makefile编写
1.Makefile的编写规则
target:prerequisites
command # command以一个tab键开始
# target为一个目标文件,可以是Object File,也可以是执行文件。还可以是一个标签(Label)
# prerequisites是需要生成target所依赖的文件或是目标
# command也就是make需要执行的命令。(任意的Shell命令)
2.编写makefile文件
main:sub.o main.c
gcc main.c sub.o -o main
sub.o:sub.c
gcc -c sub.c -o sub.o
clean:
rm *.o注意用tab键开始!
3.使用makefile编译c程序

4.在源文件目录下执行命令make clean,即可删除编译过程中产生的*.o文件

5.运行结果

总结
Make和makefile可以完成对于多个文件的维护工作,Make和makefile的关系:make是一个命令工具,是一个解释makefile中指令的命令工具,一般来说,大多数的IDE都有这个命令.
边栏推荐
- Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
- 把腾讯搬上云:云服务器 CVM 的半部进化史
- 【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试
- 【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze
- [today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out
- Prometheus 2.27.0 新特性
- MFC CString互转LPVOID
- [inverted pendulum control] Simulink simulation of inverted pendulum control based on UKF unscented Kalman filter
- Flask基础:模板渲染+模板过滤使用+控制语句
- 被通知裁员后拿到5个offer
猜你喜欢

Digital intelligence learning Lake Warehouse Integration Practice and exploration

How technicians become experts in technical field

Interpretation of the source code of scheduledthreadpoolexecutor (II)

简单ELK配置实现生产级别的日志采集和查询实践

SQL injection bypass (V)

MySQL optimization tips

字节跳动面试官:一张图片占据的内存大小是如何计算
![[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out](/img/1b/31b5adbec5182207c371a23e41baa3.png)
[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out

Moving Tencent to the cloud: half of the evolution history of cloud server CVM

【历史上的今天】6 月 20 日:MP3 之父出生;富士通成立;谷歌收购 Dropcam
随机推荐
启牛开户安全吗?怎么线上开户?
【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试
math_(函数&数列)极限的含义&误区和符号梳理/邻域&去心邻域&邻域半径
【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生
LiveData 面试题库、解答---LiveData 面试 7 连问~
Truth table of common anode digital tube
LeetCode - Easy - 197
Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
MySQL optimization tips
MFC常用 当前路径
【 amélioration de la correction d'image de Code bidimensionnel】 simulation du traitement d'amélioration de la correction d'image de Code bidimensionnel basée sur MATLAB
Opencv——霍夫变换以及遇到的一些问题
The graduation season is coming, and the number of college graduates in 2022 has exceeded 10 million for the first time
From how to use to how to implement a promise
Stm32f1 interrupt introduction
MySQL collection, here are all the contents you want
SQL reported an unusual error, which confused the new interns
Low code solution - a low code solution for digital after-sales service covering the whole process of work order, maintenance and Finance
【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
【方块编码】基于matlab的图像方块编码仿真
