久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2155|回復: 0
打印 上一主題 下一主題
收起左側

makefile相關的知識

[復制鏈接]
跳轉到指定樓層
樓主
ID:108615 發表于 2016-3-13 17:22 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
makefile的功能太過強大,完全個人總結,知 識有限,歡迎糾正錯誤!


main.c

#include "main.h"
int main()
{
        printf_hello();
        printf_ok();
        return ;
}

main.h

#ifndef _MAIN_H
#define _MAIN_H
#include "hello.h"
#include "ok.h"
#endif


printf_hello.c

#include "hello.h"
void printf_hello(void)
{
        printf("hello!\n");
        printf_thanks();
}
void printf_thanks(void)
{
        printf("thanks!!!\n");
}

hello.h

#ifndef _HELLO_H
#define _HELLO_H
#include <stdio.h>
void printf_thanks(void);
void printf_hello(void);
#endif

printf_ok.c

#include "ok.h"
void printf_ok(void)
{
        printf("ok!!!\n");
        printf_thanks();
}

ok.h

#ifndef _OK_H_
#define _OK_H_
#include <stdio.h>
void printf_ok(void);
extern void printf_thanks(void);
#endif

在編譯六個文件組成的項目時,可以不用寫makefile文件因為文件的數量比較的少所以
可以直接在linux系統下直接使用  gcc  main.c  printf_hello.c  printf_ok.c  -o  main
編譯成執行文件  main  ,可以直接./main執行此文件即可。(上面的所有的文件必須
在一個文件夾中,才可以編譯,否則 將會報錯說找不到目標文件)。

當項目比較大的時候,文件可能幾千甚至更多的時候,我們可以先創建一個工程文件名
為project的文件夾,之后在其中在創建兩個文件夾,一個取名為inc   ,另一個取名為
src 。在inc中我們通常是放.h文件,而在src中我們通常放的是.c文件和makefile文件。

一般的makefile文件如下:

main:main.o printf_hello.o printf_ok.o
        gcc main.o printf_hello.o printf_ok.o -o main
main.o:main.c
        gcc -c -I ../inc/ main.c -o main.o
printf_hello.o:printf_hello.c
        gcc -c -I ../inc/ printf_hello.c -o printf_hello.o
printf_ok.o:printf_ok.c
        gcc -c -I ../inc/ printf_ok.c -o  printf_ok.o
clean:
        -rm -f *.o main    //移除后綴為.o和目標文件

每一個gcc命令的最后加上  -g   就可以使用   gdb  編譯!!!

makefile 通常有三種寫法  Makefile ,   makefile   ,   GNU makefile
第一種使用較多,因為大寫的M的 ASCII 較小系統擺放文件時將其放在了前面。

調用make命令可輸入
target是Makefile文件中定義的目標之一,如果省略target,make就將生成
Makefile文件中定義的第一個目標。
例如:在上面的例子中我們可以輸入   make  main.o   或  make  printf_hello.o
等等  ,但是如果你輸入的是make  main.o 那么他只會執行gcc -c -I ../inc/ main.c
-o main.o   這一個命令!而單獨的一個“make”命令等價于make  main命令,
由于依賴的關系所以下面的命令都執行除了clean命令外(clean命令和他們沒有
依賴的關系)。

因為main是Makefile文件中定義的第一個目標,make首先將其讀入,然后從第一行
開始執行,把第一個目標main作為它的最終目標,所有后面的目標的更新都會影響到
main的更新。

如果直接在linux系統下敲擊make命令,則系統執行的命令是makefile,而
不是執行的是Makefile文件的命令。如果文件夾中既有makefile 也有Makefile文
件,我們需要使用make的命令,make  -f    Makefile  就可以執行Makefile的文
件的命令。而以上的makefile文件可以通過更改頭文件的路徑而更改,.h文件不必
改變。

make的執行過程:
1.如果目標文件的時問戳比依賴文件還早,就按規則中定義的命令更新目標文件。
2.如果該規則中的依賴文件又是其他規則中的目標文件,那么依照規則鏈不斷執行這
個過程,直到Makefile文件的結束,至少可以找到一個不是規則生成的最終依賴文
件,獲得此文件的時間戳
3.然后從下到上依照規則鏈執行目標文件的時間戳比此文件時間戳舊的規則,直到最
頂層的規則

#include "../inc/ok.h"    //將相對路徑添加到頭文件中
void printf_ok(void)
{
        printf("ok!!!\n");
        printf_thanks();
}

#include "../inc/hello.h"    //將相對路徑添加到頭文件中
void printf_hello(void)
{
        printf("hello!\n");
        printf_thanks();
}
void printf_thanks(void)
{
        printf("thanks!!!\n");
}

#include "../inc/main.h"   //將相對路徑添加到頭文件中
int main()
{
        printf_hello();
        printf_ok();
        return ;
}

修改后的makefile文件:
main:main.o printf_hello.o printf_ok.o
        gcc main.o printf_hello.o printf_ok.o -o main
main.o:main.c
        gcc -c  main.c -o main.o
printf_hello.o:printf_hello.c
        gcc -c printf_hello.c -o printf_hello.o
printf_ok.o:printf_ok.c
        gcc -c printf_ok.c -o  printf_ok.o
clean:
        -rm -f *.o main

Makefile的變量
(1)變量名不包括: “   :”  、“   #   ”  、“  =  ”
(2)變量名是大小寫敏感的,“  foo ”與“ Foo ”代表不同的變量
(3)預留大寫字母作為作為控制隱含規則參數或者用戶重載命令選項

Makefile中的變量分為:
            用戶自定義變量
            預定義變量、
            自動變量

1.用戶自定義的變量
例如將上面的makefile改變:
A=main.o printf_hello.o printf_ok.o
B=gcc

main:$(A)
        $(B)  $(A) -o main
main.o:main.c
        $(B)  -c  main.c -o main.o
printf_hello.o:printf_hello.c
        $(B)  -c printf_hello.c -o printf_hello.o
printf_ok.o:printf_ok.c
        $(B)  -c printf_ok.c -o  printf_ok.o
clean:
        -rm -f *.o main

2.預定義變量
A=main.o printf_hello.o printf_ok.o
B=gcc
$@     目標文件的完整名稱
$<      第一個依賴的文件名稱

main:$(A)
        $(B)  $(A) -o $@
main.o:main.c
        $(B)  -c   $<  -o  $@
printf_hello.o:printf_hello.c
        $(B)  -c  $<  -o  $@
printf_ok.o:printf_ok.c
        $(B)  -c  $<  -o   $@
clean:
        -rm -f *.o main

也可以寫成:
main:$(A)
        $(B)  -o $@   $(A)
main.o:main.c
        $(B)  -c    -o   $@      $<
printf_hello.o:printf_hello.c
        $(B)  -c    -o   $@      $<
printf_ok.o:printf_ok.c
        $(B)  -c    -o   $@      $<
clean:
        -rm -f *.o main

3. 自動變量
實際上,make可以使工作更加自動化,也就是說,make知道一些默認
的動作,它有一些稱作隱含規則的內置的規則。
如上例完全可以寫成:
A=main.o printf_hello.o printf_ok.o
B=gcc
$@     目標文件的完整名稱
$<      第一個依賴的文件名稱

main:$(A)
        $(B)  $(A) -o $@
clean:
        -rm -f *.o main

可以省略最后兩條,因為Makefile的隱式規則指出都可由“ .c ”文件使用
以下命令生成:
$(B) $(CFLAGS)  $(CPPFLAGS)  -c   –o  file.o file.c

CFLAGS   CPPFLAGS  是系統的默認的變量為空  ,我們可以給他賦值,也
可以不理會!!!

makefile還有一種模式規則可以將makefile變更為:
A=main.o printf_hello.o printf_ok.o
B=gcc

main:$(A)
        $(B)  -o  $@  $(A)
%.o:%.c                                //代表了一系列這樣的文件
        $(B)  -c    -o   $@      $<
clean:
        -rm -f *.o main
1.模式規則是用來定義相同處理規則的多個文件的,模式規則可以引入用戶自定義
變量,為多個文件建立相同的規則,從而簡化Makefile的編寫。
2.模式規則中相關文件前必須用“    %    ”



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 精品日韩一区二区 | 亚洲视频在线看 | 亚洲成人高清 | heyzo在线| xx性欧美肥妇精品久久久久久 | 综合第一页 | 久草热视频 | 久久tv在线观看 | 亚洲国产精品激情在线观看 | 亚洲国产一区二区三区在线观看 | 亚洲欧美精品国产一级在线 | 久色视频在线观看 | 成人在线视 | 中文字幕在线观看视频网站 | 色眯眯视频在线观看 | 亚洲三级在线观看 | 丝袜一区二区三区 | 国产精品免费观看 | 午夜欧美一区二区三区在线播放 | 久夜精品 | 国产一二三区电影 | 日本视频免费观看 | 在线观看深夜视频 | 亚洲欧美日韩在线 | 国产精品爱久久久久久久 | 日本黄视频在线观看 | 亚洲一级黄色 | 美女国内精品自产拍在线播放 | 一区二区三区观看视频 | 成人一级黄色毛片 | 久久精品亚洲国产奇米99 | 国产成人精品一区二 | 欧美精品一区二区在线观看 | 一级黄色影片在线观看 | 91视频国产一区 | 一区二区三区精品视频 | 中文字幕精品一区二区三区精品 | 国产区免费视频 | 国产日韩一区二区三免费 | 亚洲精品一区二区三区 | 精品久久久久久一区二区 |