当前位置:网站首页>Makefile文件是什麼?
Makefile文件是什麼?
2022-04-23 01:48:00 【帥那個帥】
初次接觸 Makefile 還是在跟朋友一起寫 Go 項目的時候,他給我們介紹的,第一感覺聽說,感覺挺好用,作為一個碼農,對於未知的技術,應該保持好奇心,管它三七二十一,先了解了解,用起來再說
下面引用了一堆官方介紹:
很多 Linux (Unix) 做開發的初學者不了解 Makefile 是什麼,甚至大部分 Windows 開發工程師對 Makefile 都特別陌生。這個其實很正常,如果你是在 Windows 下作開發的話不需要去考慮這個問題,因為 Windows 下的集成開發環境(IDE)已經內置了 Makefile,或者說會自動生成 Makefile,我們不用去手動編寫。
Linux 中卻不能這樣,需要我們去手動的完成這項工作。Linux 下可以學習的開發語言有很多,常見的有 C/C++ 語言、python、java 等等。如果你想要在 Linux (Unix) 下做開發的話,不了解 Makefile 是一件非常失敗的事情,甚至說你就成為不了一個合格的 Linux 開發工程師。不懂 Makefile,就操作不了多文件編程,就完成不了相對於大的工程項目的操作。如果你想在 Linux (Unix) 環境下做開發的話,Makefile 是必須掌握的一項技能。
那麼,究竟什麼是 Makefile 呢?
Makefile 可以簡單的認為是一個工程文件的編譯規則,描述了整個工程的編譯和鏈接等規則。其中包含了那些文件需要編譯,那些文件不需要編譯,那些文件需要先編譯,那些文件需要後編譯,那些文件需要重建等等。編譯整個工程需要涉及到的,在 Makefile 中都可以進行描述。換句話說,Makefile 可以使得我們的項目工程的編譯變得自動化,不需要每次都手動輸入一堆源文件和參數。
複制了一堆了,總結一下:
make 介紹
make 是一個構建自動化工具,會在當前目錄下尋找 Makefile 或 makefile 文件。如果存在相應的文件,它就會依據其中定義好的規則完成構建任務。
Makefile 介紹
我們可以把 Makefile 簡單理解為它定義了一個項目文件的編譯規則。借助 Makefile 我們在編譯過程中不再需要每次手動輸入編譯的命令和編譯的參數,可以極大簡化項目編譯過程。同時使用 Makefile 也可以在項目中確定具體的編譯規則和流程,很多開源項目中都會定義 Makefile 文件。
廢話不多說了,趕緊為 Go 項目編寫 Makefile
規則概述
Makefile 由多條規則組成,每條規則主要由兩個部分組成,分別是依賴的關系和執行的命令。
其結構如下所示:
[target] ... : [prerequisites] ...
<tab>[command]
...
...
其中:
- targets:規則的目標
- prerequisites:可選的要生成 targets 需要的文件或者是目標。
- command:make 需要執行的命令(任意的 shell 命令)。可以有多條命令,每一條命令占一行。
build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o xx
示例
.PHONY: all build run gotool clean help
BINARY="myapp"
all: gotool build
build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${
BINARY}
run:
@go run ./
gotool:
go fmt ./
go vet ./
clean:
@if [ -f ${
BINARY} ] ; then rm ${
BINARY} ; fi
help:
@echo "make - 格式化 Go 代碼, 並編譯生成二進制文件"
@echo "make build - 編譯 Go 代碼, 生成二進制文件"
@echo "make run - 直接運行 Go 代碼"
@echo "make clean - 移除二進制文件和 vim swap files"
@echo "make gotool - 運行 Go 工具 'fmt' and 'vet'"
或者
.PHONY:clean
clean:
rm -rf *.o test
.PHONY:run
run:
@go run ./
- BINARY=”myapp” 是定義變量。
- PHONY 用來定義偽目標。不創建目標文件,而是去執行這個目標下面的命令。
- @符號就是代錶執行此命令但是不用輸出該命令
- clean 命令可以看出可以支持 shell 命令
版权声明
本文为[帥那個帥]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230147258063.html
边栏推荐
- 电子采购如何成为供应链中的增值功能?
- Rôles attributs personnels Abréviations
- 最长公共子序列(记录路径版)
- MySQL active / standby configuration binary log problem
- Keil MDK Chinese garbled code, two solutions, the font is no longer ugly
- 如何选择一台好的拨号服务器?
- 紫光国微财报一枝独秀 2021年净利润三位数增长靠什么
- .NET单元测试第一篇:常见.NET单元测试框架有哪些?
- 2022第六季完美童模 IPA國民賽領跑元宇宙賽道
- Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)
猜你喜欢
什么时候应该编写单元测试?什么是TDD?
CC2541的仿真器CC Debugger使用教程
Performance introduction of the first new version of cdr2022
Question bank and online simulation examination for safety management personnel of hazardous chemical business units in 2022
Server 2019 the available memory of the server is half of the actual memory
单片机和4G模块通信总结(EC20)
紫光国微财报一枝独秀 2021年净利润三位数增长靠什么
DO447管理用户和团队的访问
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
Error in face detection and signature of Tencent cloud interface
随机推荐
Echo "new password" |passwd -- stdin user name
2n queen problem
《维C中国》乡村助农暖人心第三站嘉宝果农场
Encrypted compressed backup bat script
Futr3d: a unified 3D detection framework for sensor fusion
The most easy to understand service container and scope of dependency injection
2022 melting welding and thermal cutting operation certificate examination question simulation examination platform operation
W801 / w800 WiFi socket development (I) - UDP
postman里面使用 xdebug 断点调试
Performance introduction of the first new version of cdr2022
npm——配置淘宝镜像
W801/W800/W806唯一ID/CPUID/FLASHID
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
Realize the function of progress bar through layerdrawable
Challenges often faced by client project management
2022第六季完美童模 IPA国民赛领跑元宇宙赛道
Tight coupling of visual wheel odometer
[Blue Bridge Cup] [10th real topic in 2019] priority of takeout shop
Jisuan Hakka spectrum (DFS for direct post algebra)
Error in face detection and signature of Tencent cloud interface