日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

makefile的使用方法(简单视频教程以及详细文字教程)

發布時間:2025/3/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 makefile的使用方法(简单视频教程以及详细文字教程) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1、makefile詳細文字教程
  • 2、makefile簡單教程(B站視頻教程)
    • makefile基本語法格式:
    • 簡單示例(單文件)makefile寫法
    • 帶包含關系的多文件makefile寫法(包含1個文件)
    • 在需要發布源碼時,編譯后刪除可執行文件和.o鏈接文件的makefile指令(make clean)
    • 帶包含關系的多文件makefile寫法(包含>=2個文件)
    • makefile指定編譯器CC(名字自己定)
    • 增加自定義參數
    • 多個主函數編譯生成多個可執行文件的makefile寫法(all指令)

1、makefile詳細文字教程

Linux makefile 教程 非常詳細,且易懂

原文:跟我一起寫 Makefile(有什么不懂可以跳轉到原文評論區答疑解惑)

2、makefile簡單教程(B站視頻教程)

參考教程視頻鏈接:教程:Makefile的寫法

makefile基本語法格式:

簡單示例(單文件)makefile寫法

新建文件main.cpp

#include <iostream>using namespace std;int main(){cout << "success!" << endl;return 0; }

新建文件makefile,跟.cpp文件放一起

main:main.cg++ main.c -o main

終端運行

make

結果:

帶包含關系的多文件makefile寫法(包含1個文件)

yg@ubuntu:~/arnold_test/20210911_makefileTest2$ tree . ├── main.c ├── makefile ├── tool.c └── tool.h

main.c

#include "tool.h" #include <stdio.h>int main(){int arr[] = {1,8,3,4,5};int m = find_max(arr,5);printf("max = %d\n",m);return 0; }

tool.c

#include "tool.h"int find_max(int arr[], int n){int m = arr[0];int i;for(i=0; i<n; i++){if(arr[i] > m){m = arr[i];}}return m; }

tool.h

#pragma onceextern int find_max(int arr[], int n);

多文件時,寫makefile一般從后往前寫

makefile

main:main.c tool.ogcc main.c tool.o -o maintool.o:tool.cgcc -c tool.c

編譯:

yg@ubuntu:~/arnold_test/20210911_makefileTest2$ make gcc -c tool.c gcc main.c tool.o -o main yg@ubuntu:~/arnold_test/20210911_makefileTest2$ yg@ubuntu:~/arnold_test/20210911_makefileTest2$ tree . ├── main ├── main.c ├── makefile ├── tool.c ├── tool.h └── tool.o

.o是鏈接文件

在需要發布源碼時,編譯后刪除可執行文件和.o鏈接文件的makefile指令(make clean)

在后面加上這一句:

clean:rm *.o main

makefile

main:main.c tool.ogcc main.c tool.o -o maintool.o:tool.cgcc -c tool.cclean:rm *.o main

這樣在我們使用make指令編譯后,再使用make clean指令,就能將生成的main和tool.o刪除

yg@ubuntu:~/arnold_test/20210911_makefileTest2$ make clean rm *.o main yg@ubuntu:~/arnold_test/20210911_makefileTest2$ tree . ├── main.c ├── makefile ├── tool.c └── tool.h0 directories, 4 files yg@ubuntu:~/arnold_test/20210911_makefileTest2$

帶包含關系的多文件makefile寫法(包含>=2個文件)

yg@ubuntu:~/arnold_test/20210911_makefileTest2$ tree . ├── bar.c ├── bar.h ├── foo.c ├── foo.h ├── main.c └── makefile0 directories, 6 files yg@ubuntu:~/arnold_test/20210911_makefileTest2$

main.c

#include "foo.h" #include "bar.h" #include <stdio.h>int main(){int arr[] = {1,2,3,4,5};printf("min = %d\n", find_min(arr, 5));printf("max = %d\n", find_max(arr, 5)); return 0; }

bar.c

#include "bar.h"int find_min(int arr[], int n){return 1; }

foo.c

#include "foo.h"int find_max(int arr[], int n){return 10; }

bar.h

int find_min(int arr[], int n);

foo.h

int find_max(int arr[], int n);

makefile

main:main.c bar.o foo.ogcc main.c bar.o foo.o -o mainbar.o:bar.cgcc -c bar.cfoo.o:foo.cgcc -c foo.cclean:rm *.o main

運行結果:

yg@ubuntu:~/arnold_test/20210911_makefileTest2$ make gcc -c bar.c gcc -c foo.c gcc main.c bar.o foo.o -o main yg@ubuntu:~/arnold_test/20210911_makefileTest2$ ls bar.c bar.h bar.o foo.c foo.h foo.o main main.c makefile yg@ubuntu:~/arnold_test/20210911_makefileTest2$ make clean rm *.o main yg@ubuntu:~/arnold_test/20210911_makefileTest2$

makefile指定編譯器CC(名字自己定)

makefile

CC = gccmain:main.c bar.o foo.o$(CC) main.c bar.o foo.o -o mainbar.o:bar.c$(CC) -c bar.cfoo.o:foo.c$(CC) -c foo.cclean:rm *.o main

增加自定義參數

多個主函數編譯生成多個可執行文件的makefile寫法(all指令)


代碼略

makefile

CC = gccall:main_min main_maxmain_min:main_min.c bar.o$(CC) main_min.c bar.o -o main_minmain_max:main_max.c foo.o$(CC) main_max.c foo.o -o main_maxbar.o:bar.c$(CC) -c bar.cfoo.o:foo.c$(CC) -c foo.cclean:rm *.o main_min main_max

運行結果:

yg@ubuntu:~/arnold_test/20210911_makefileTest2$ make gcc -c bar.c gcc main_min.c bar.o -o main_min gcc -c foo.c gcc main_max.c foo.o -o main_max yg@ubuntu:~/arnold_test/20210911_makefileTest2$ tree . ├── bar.c ├── bar.h ├── bar.o ├── foo.c ├── foo.h ├── foo.o ├── main_max ├── main_max.c ├── main_min ├── main_min.c └── makefile0 directories, 11 files yg@ubuntu:~/arnold_test/20210911_makefileTest2$ make clean rm *.o main_min main_max yg@ubuntu:~/arnold_test/20210911_makefileTest2$ ls bar.c bar.h foo.c foo.h main_max.c main_min.c makefile yg@ubuntu:~/arnold_test/20210911_makefileTest2$

總結

以上是生活随笔為你收集整理的makefile的使用方法(简单视频教程以及详细文字教程)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。