经常在项目代码中能看到:"Makefile" 这个文件,这个文件到底是啥?有何作用?一文详解!

分类: 365体育投注网址亚洲下载 时间: 2026-10-13 04:52:23 作者: admin 观测: 9326
经常在项目代码中能看到:

Makefile 本质上是一个自动化构建脚本,主要用于定义:如何编译、构建、运行、测试、清理一个项目。

它最早来自 C/C++ 项目,但现在在 Python、Go、Rust、前端项目、AI 工程里也非常常见。

简单理解:

Makefile = 把一堆你经常执行的命令写成快捷指令。

比如你平时可能需要:

python setup.py install

python train.py

docker build -t myapp .

pytest tests/

rm -rf build/

这些命令如果经常执行,就可以写进 Makefile:

install:

pip install -r requirements.txt

train:

python train.py

docker:

docker build -t myapp .

test:

pytest tests/

clean:

rm -rf build/

以后直接:

make install

等价于执行:

pip install -r requirements.txt

1. Makefile 最经典用途:编译 C/C++

比如一个 C 项目:

目录:

project/

├── main.c

├── utils.c

├── utils.h

└── Makefile

没有 Makefile:

gcc main.c utils.c -o app

有 Makefile:

app:

gcc main.c utils.c -o app

运行:

make

自动生成:

app

2. Makefile 的核心概念:目标(target)

基本格式:

目标: 依赖

命令

注意命令前必须是 Tab,不是空格。

例如:

hello:

echo "hello world"

执行:

make hello

输出:

hello world

3. 依赖关系

比如:

app: main.o utils.o

gcc main.o utils.o -o app

意思:

生成 app 前,需要:

main.o

utils.o

如果不存在,先生成:

main.o:

gcc -c main.c

utils.o:

gcc -c utils.c

形成:

main.c

|

main.o

\

---> app

/

utils.o

|

utils.c

4. AI / 大模型项目里 Makefile 常见用途

现在 AI 工程里特别常见。

比如随便列举如下项目:

MCP Gateway

FastAPI 服务

TTS 服务

RAGFlow

Docker 部署

都可能有 Makefile。

例如:

该 MCP Gateway:

mcp_gateway/

├── server/

├── config/

├── Dockerfile

├── requirements.txt

└── Makefile

Makefile:

install:

pip install -r requirements.txt

run:

python server/fastmcp_gateway.py

docker-build:

docker build -t mcp-gateway .

docker-run:

docker run -p 8081:8081 mcp-gateway

logs:

docker logs -f mcp-gateway

使用:

启动:

make run

构建:

make docker-build

查看日志:

make logs

5. 为什么不用 Shell 脚本?

你可能会问:

那我写一个 run.sh 不就行了?

确实可以。

区别:

shell:

./run.sh

偏向:

一次性执行

流程脚本

Makefile:

make train

make test

make deploy

偏向:

项目管理

多任务入口

例如:

大型项目:

make install

make format

make lint

make test

make build

make deploy

非常统一。

6. Docker 项目里面特别常见

比如:

build:

docker compose build

up:

docker compose up -d

down:

docker compose down

restart:

docker compose restart

以后:

启动整个系统:

make up

停止:

make down

7. Python 项目里的 Makefile

例如:

my-agent/

├── src/

├── tests/

├── requirements.txt

└── Makefile

setup:

pip install -r requirements.txt

format:

black .

test:

pytest

start:

python main.py

开发人员:

make setup

make test

make start

不用记几十条命令。

8. 查看一个项目支持哪些 make 命令

很多开源项目:

README:

make help

或者:

make

会显示:

Available commands:

make install

make test

make build

make deploy

9. 接触的项目里为什么经常看到?

结合常见的项目:

MCP Gateway

RAGFlow

GPT-SoVITS

CosyVoice

Docker

vLLM

这些都是工程项目。

开源作者通常希望:

别人 clone 后:

git clone xxx

cd xxx

make install

make run

而不是:

conda create ...

pip install ...

修改配置...

export变量...

python xxx.py ...

docker xxx...

所以 Makefile 相当于项目的统一操作入口。

一句话总结:

Makefile 就是项目的“快捷命令菜单”,把安装、启动、测试、编译、部署等复杂命令封装起来,让开发者用 make xxx 一键执行。

对于常见的 AI 工程(MCP、RAG、TTS、Agent),看到 Makefile 基本可以理解为:这个项目提供了一套标准化的工程操作入口。

← 如何使用anki和supermemo学习数学物理编程等科目并准备考试?记忆软件是否不适合这些科目? - @Thoughts Memo 超声波换能器经常发热该如何解决 40khz是什么意思? →

相关时空节点

ENVI中从图像中获取指定点的像素值

ENVI中从图像中获取指定点的像素值

07-10 💫 230
2025年公众号粉丝多少才能接广告?公众号一万粉丝接一条广告多少钱(最新指南)

2025年公众号粉丝多少才能接广告?公众号一万粉丝接一条广告多少钱(最新指南)

01-19 💫 976
华山旅游景点有哪些?陕西渭南市十大著名景点介绍与攻略

华山旅游景点有哪些?陕西渭南市十大著名景点介绍与攻略

10-03 💫 323