makefile编译helloword

 

hello.c文件:

1
2
3
4
5
6
#include <stdio.h>
 
int main() {
    printf("Hello Word!!!\n");
    return 0;
}

 

 

Makefile文件:

  执行顺序,

    1.hello:hello.o中的hello是要生成的执行文件,谁来生成呢? 就是hello.o。

    2.如果没有hello.o文件呢?make就会自动在文件里寻找依赖关系(自动推导),

        看hello.o怎么生成的,就是“hello.o:hello.c”。

    3.以此类推的一个递归。

    4.找到依赖关系就会执行下面的cc那条语句

  注意:1.hello:hello.o和hello.o:hello.c 的位置不可互换,

         make默认把第一个target做为生成第一目标,也就是hello:hello.o里的hello(第一目标或者可以理解为makefile的入口)

     2.Makefile执行如果报错,可能就是行的缩进问题,Makefile这点很像Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#first makefile
 
hello:hello.o
    cc -o hello  hello.o
 
hello.o:hello.c
    cc -c -o hello.o hello.c
 
clean :
    @echo " "
        ifeq (,$(wildcard ./hello))
        @echo "'hello' file is not exist."
        else
        @echo "'hello' file is exist."
        rm hello
        endif
    @echo " "
        ifeq (,$(wildcard ./hello.o))
        @echo "'hello.o' file is not exist."
        else
        @echo "'hello.o' file is exist."
        rm hello.o
        endif
 
    @echo "\nClean done."

 

执行make编译(make 会自动寻找当前目录下名字为Makefile名字的文件)

1
$ make

下图是执行make编译完成后,产生了hello可执行文件

(下图绿色的hello为可执行文件,没错,Linux下可执行文件不需要有后缀)

 

 

执行make clean命令:

1
$ make clean

   执行了rm 删除文件

 

 

 

为什么Makefile里用CC ,而命令行用gcc?

答:(这里引用网上的一段话)gcc是C编译器;g++是C++编译器;linux下cc一般是一个符号连接,指向gcc;gcc和g++都是GUN(组织)的编译器。而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Linux/Unix都是大小写敏感的系统,这点一定要注意。

 

 

参考:

https://www.cnblogs.com/henuliulei/p/9918054.html

https://www.cnblogs.com/xj626852095/p/3648246.html

posted @   悟透  阅读(436)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示