linux项目构建工具——makefile

简介: 对于一些简单的makefile中的指令对的简介

@TOC

一、 makefile

在多文件中使用

1.创建文件

先创建三个文件 test.h   mytest.c   main.c 文件

[yzq@VM-8-8-centos mk]$  touch test.h mytets.c main.c
[yzq@VM-8-8-centos mk]$  ls
main.c  mytets.c  test.h

2. test.h ——函数的定义

使用 vim test.h 进入vim编辑器

这里要使用条件编译,`#ifndef TEST    #define TEST #endif

如果TEST 未被定义,则进入,#deifne定义 TEST endif 结束

这样做是为了防止头文件被包含

#ifndef TEST
#define TEST
#include<stdio.h>
void show();
#endif

定义函数show和c语言库

3. mytest.c——函数的实现

#include"test.h"
void show()
{
 int i=0; 
 for(i=;i<=10;i++)
 {
 printf("hello world\n");
 }
}

这里需要注意的是,我们引用的头文件是 test.h ,因为是自己创建的头文件,所以要用" "

4. main.c——函数的调用

#include"test.h"
int main()
{
 show();
 return 0;
}

这里只需调用show函数即可

5. 正常生成

[yzq@VM-8-8-centos mk]$  ls
main.c  mytest.c  mytets.c  test.h
[yzq@VM-8-8-centos mk]$  gcc mytest.c main.c -o test
[yzq@VM-8-8-centos mk]$  ls
main.c  mytest.c  mytets.c  test  test.h
[yzq@VM-8-8-centos mk]$  ./test
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

正常生成是利用 gcc mytest.c main.c -o test

生成一个可执行程序 test

./ test产生 hello world

6. makefile的使用

首先使用 vim makefile (这里若是没有创建,则会自动创建一个文件)进入vim编辑器

test: mytest.c main.c
    gcc $^ -o $@
.PHONY:clean
    clean: rm -f test

输入ESC  :wq,退出 vim 编辑器

[yzq@VM-8-8-centos mk]$  make
gcc mytest.c main.c -o test
[yzq@VM-8-8-centos mk]$  ./test
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
[yzq@VM-8-8-centos mk]$  make clean
rm -f test

输入make 会自动生成gcc mytest.c main.c -o test ,并使用 ./test

输入 make clean 会自动生成 rm -f test ,可以自动删除目标文件test

1. 解析

test : mytest .c main.c

test 作为目标文件 ,冒号后的mytest.c 与main.c是依赖文件,这句话整体作为一个依赖关系

gcc $^ -o $@

$^代表目标文件

$@代表 依赖文件

等价于 gcc mytest.c main.c -o test

这句话整体作为一个依赖方法

特别注意:在gcc 前面加上 TAB

.PHONY:clean

  clean: rm -f test

.PHONY可以看作是makefile的关键字

clean ——伪目标

clean则是作为 伪目标存在

伪目标特点

伪目标特点:总是被执行

[yzq@VM-8-8-centos mk]$ make
gcc mytest.c main.c -o test
[yzq@VM-8-8-centos mk]$ make
make: `test' is up to date.
[yzq@VM-8-8-centos mk]$ make
make: `test' is up to date.

当多次使用make时,发现只有第一次可以运行,其他就会报错

[yzq@VM-8-8-centos mk]$ make clean
rm -f test
[yzq@VM-8-8-centos mk]$ make clean
rm -f test
[yzq@VM-8-8-centos mk]$ make clean
rm -f test
[yzq@VM-8-8-centos mk]$ make clean
rm -f test

当多次使用 make clean时,发现可以都可以正常运行。

使用 make clean 的原因

makefile是一个脚本,默认识别是从上往下,只会执行一个可执行,所以想要跳过项目的创建,就要加上对应的名字

目录
相关文章
|
15天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
1月前
|
Linux 编译器 开发工具
Linux学习第二枪(yum,vim,g++/gcc,makefile的使用)
Linux学习第二枪(yum,vim,g++/gcc,makefile的使用)
|
13天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
37 6
|
1月前
|
IDE Linux 编译器
【Linux】项目自动化构建工具 —— make/Makefile
【Linux】项目自动化构建工具 —— make/Makefile
|
1月前
|
IDE 小程序 Linux
【linux】Linux项目自动化构建工具-make/Makefile
【linux】Linux项目自动化构建工具-make/Makefile
26 0
|
1天前
|
Linux 开发工具 C语言
【linux基础(七)】Linux中的开发工具(下)--make/makefile和git
【linux基础(七)】Linux中的开发工具(下)--make/makefile和git
|
1月前
|
消息中间件 并行计算 网络协议
探秘高效Linux C/C++项目架构:让进程、线程和通信方式助力你的代码飞跃
探秘高效Linux C/C++项目架构:让进程、线程和通信方式助力你的代码飞跃
34 0
|
1月前
|
Shell Linux 编译器
Linux Makefile 全面教程:使用 Makefile 进行项目管理和构建
Linux Makefile 全面教程:使用 Makefile 进行项目管理和构建
37 0
|
1月前
|
Linux 编译器 开发工具
『Linux升级路』基础开发工具——make/Makefile篇
『Linux升级路』基础开发工具——make/Makefile篇
|
1月前
|
Linux 开发者
linux系统中Makefile的基本使用方法
linux系统中Makefile的基本使用方法
29 1