makefile 指定include_Linux下C语言编程实践(Makefile入门)

        在Linux下时不时会遇到一个命令“make”,这个命令到底是什么,怎么感觉Linux下好多软件在源码编译安装的时候都会用到它,甚至编译内核也会用到它。除了make还有make installmake clean等,反正每次按照网上教程敲命令就好了。从来没有关心过make是干什么的?你是属于这种情况吗?如果是,赶快认真学一学下面的内容吧!

        在软件开发中,make通常被视为一种软件构建工具。该工具主要经由读取一种名为“makefile”或“Makefile”的文件来实现软件的自动化建构。

Makefile

        make命令执行时,需要一个 makefile 文件,以告诉make命令如何去编译程序。

        一个大型的工程项目中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中。makefile定义了一系列的规则来指定工程项目中哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译。

我以前写的程序都只有一个源文件,难怪我从来没用过这么高级的工具

写一个求圆的周长和面积的程序(v1)

   一个文件一个函数

#include
#define PI 3.141592653

int main(){
    double r;
    printf("Please enter the radius of the circle: ");
    scanf("%lf",&r);
    printf("Circumference of the circle is: ");
    printf("%lf\n",r*PI*2);
    printf("Area of the circle is: ");
    printf("%lf\n",r*r*PI);
    return 0;
}

编译运行方式:

cc9714301ef4e16b5a634002cfd1b294.png

写一个求圆的周长和面积的程序(v2)

   一个文件多个函数。

#include
#define PI 3.141592653
double  get_circumference(double r);
double  get_area(double r);
int main(){
    double r;
    printf("Please enter the radius of the circle ");
    scanf("%lf",&r);
    printf("Circumference of the circle is: ");
    printf("%lf\n",get_circumference(r));
    printf("Area of the circle is: ");
    printf("%lf\n",get_area(r));
    return 0;
}

double  get_circumference(double r){
     return  2*PI*r;
}

double  get_area(double r){
     return  PI*r*r;
}

编译运行方式:

ff16e67052119178756bb05c1bc7b4d7.png

写一个求圆的周长和面积的程序(v3)

多个文件多个函数。

文件: MyPrograme.c

#include 
#include "mylib.h"

int main(){
    double r;
    printf("Please enter the radius of the circle:\n");
    scanf("%lf",&r);
    printf("Circumferenceof the circle is:\n");
    printf("%lf\n",get_circumference(r));
    printf("Area of the circleis:\n");
    printf("%lf\n",get_area(r));
    return 0;
}

文件: Mylib.h

#define  PI 3.141592653
double  get_circumference(double r);
double  get_area(double r);

文件: Mylib.c

#include "mylib.h"double  get_circumference(double r){

596661cf5548be107f168da50d025bcc.png

编译运行方式:

5e16fb53f91119c9f254bacea97deef0.png

另外一种编译方式:

8398c360243ba483bc382244e9d4e00f.png

上面的编译方式,当我们修改一个文件时,不需要所有的源文件都参与编译,只需要编译改动的那个文件即可,然后最终编译为可执行文件。但是如果源文件很多,那就比较麻烦了,因此需要下面的Make了。

写一个求圆的周长和面积的程序(v4)

使用Make管理项目

makefile文件内容

hello : mylib.o  myprograme.o
        gcc -o hello mylib.o myprograme.o

mylib.o : mylib.c mylib.h
        gcc -c mylib.c

myprograme.o: myprograme.c
        gcc -c myprograme.c

clean:
        rm hello mylib.o myprograme.o

操作步骤

996597ce3258b3f7e1922dc8be612c2c.png

以上的makefile文件,还非常的不标准,仅仅是为了给大家一个最容易理解的示例。大家可以先查阅资料,了解更过的内容。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值