makefile簡介及其簡單編寫示例

2021-07-09 08:04:41 字數 3546 閱讀 3355

makefile檔案裡面主要有三種內容:

1.變數宣告:

變數宣告就是一種基本的嚴格字元替換的操作。

比如在前面宣告了:objects=program.o foo.o utils.o

那麼在後面出現的所有$(objects)或者$都會被自動替換成上面的那個字串行,而且是嚴格替換,即不帶空格的。

2.對映法則

3.命令:

對映法則和命令通常都是聯合起來組成這樣的結構形式:

target... : prerequisites..

command

可以簡單地理解為通過prerequisites,也就是先決的依賴檔案,採取後面描述的相應的命令(這裡的命令都是linux裡的shell命令)command之後(一般是)生成了檔案target。命令的前面都要按以下tab建留一段空白來表示它是命令。

有的target後面並沒有先決條件,也就是後面的命令是無條件執行的。

makefile 檔案c語言程式:

#include

#include

module_license("dual bsd/gpl");

static int hello_init(void)

static void hello_exit(void)

module_init(hello_init);

module_exit(hello_exit);

makefile:

# if kernelrelease is defined, we've been invoked from the

# kernel build system and can use its language.

ifneq ($(kernelrelease),)

obj-m := hello.o 

# otherwise we were called directly from the command

# line; invoke the kernel build system.

else

kerneldir ?= /lib/modules/$(shell uname -r)/build

pwd := $(shell pwd) 

default:

$(make) -c $(kerneldir) m=$(pwd) modules

endif

makefile

裡面所寫的內容其實就是你要編譯的命令,那麼,什麼是編譯命令呢?

假寫你已經寫好乙個程式**,並將之存在乙個

.c檔案

中,如:

hello.c,

在終端上你可以這樣做!在終端上輸入

gcc -o hello hello.c

然後回車,看一看有沒有什麼反映,如果沒有打出很多英文的話,恭喜你!你完美地完成了第一步!然後,在終端中輸入

./hello

看看是不是有什麼輸出了?

現在來解釋一下編譯命令:上面的命令的意思就是,使用

gcc編譯器編譯

hello.c源**

,生成的檔名稱叫做

hello.

最後,要看程式執行結果,就要執行生成的程式也就是

「./hello」

了,「./」

的意思就是在當前的目錄下執行。 而

makefile

中內容的就是上面的編譯命令,如:在

makefile

檔案中寫入

hello:hello.c

gcc -o hello hello.c

儲存檔案之後直接在終端中輸入

make

,就完成編譯了!

makefile

存在的意義只是讓編譯更加方便,也就說,可以把所以的編譯都寫在乙個

makefile

檔案中,然後在終端中輸入

make

就可以完成

makefile

檔案裡的命令!

怎麼寫這個程式的makefile檔案

#include

#include "sin_value.h"

#include "cos_value.h"

#include "haha.h"

#define pi 3.14159

char name[15];

float angle;

int main(void)

這是main.c檔案,其中的sin_value.h,cos_value.h,haha.h分別如下:

#include

#include

#define pi 3.14159

void sin_value(float angle)

//這是sin_value.h

#include

#include

#define pi 3.14159

void cos_value(float angle)

//這是cos_value.h

#include

int haha(char name[15])

//這是haha.h

程式沒有問題,不用makefile的方法我已經執行通過了,麻煩直接給出可以copy的makefile,不要給教程什麼,拜謝!!!另外是不是這個makefile檔案要放在程式那個資料夾裡啊?

提問者採納

makefile

檔案名字,放在這個你這個程式的目錄中:

objs=sin_value.o cos_value.o haha.o main.c

ldflags:=  #

這個是編譯引數例:

-lpthread

執行緒庫

cflags:= -g -wall #

可調式,以及編譯警告通知

all:$(prog)

sin_value.o:sin_value.c

$(cxx) $(cflags) -c -o $@ $^ 

cos_value.o

:cos_value.c

$(cxx) $(cflags) -c -o $@ $^ 

haha.o: haha.c

$(cxx) $(cflags) -c -o $@ $^ 

main.o:main.c

$(cxx) $(cflags) -c -o $@ $^ 

install:

cp $(prog) /bin

clean:

rm -rf *.o  *~

rm -rf $(prog)

rm -rf /bin

/$(prog)

$(prog):$(objs)

$(cxx) $(ldflags) -o $@ $^

[精華]跟我一起寫makefile

linux/unix環境下的make和makefile詳解

教會你如何編寫makefile檔案

source檔案和makefile檔案編寫

一.makefile 沒有副檔名,它名字就叫makefile 內容如下 include ntmakeenv makefile.def wdm程式使用的所有makefile都這樣寫,我們只需寫乙個,編譯時把它拷貝到工作目錄下就行了 二.sources檔案就需要我們根據不同的場合修改了,不過基本模板如下...

ant呼叫make實現Makefile編譯

為了讓ant能執行make,還得用指令碼實現 linux sh指令碼實現,build.sh bin sh export build folder cd dirname 0 pwd prj 判斷makefile是否存在,如果不存在,則呼叫newprj.sh生成makefile if r build f...

乙個簡單的makefile示例及其注釋

因為專案開發需要,要學會寫makefile,遂搗之,在網上看到一篇很不錯的makefile的入門文章,轉來和大家分享 相信在unix下程式設計的沒有不知道makefile的,剛開始學習unix平台 下的東西,了解了下makefile的製作,覺得有點東西可以記錄下。下面是乙個極其簡單的例子 現在我要編...