乙個Makefile例子

2021-10-07 23:06:26 字數 2060 閱讀 5667

main.c:

#include

"log.h"

intmain()

sum.c

#include

"log.h"

void

sum(

)

sub.c

#include

"log.h"

void

sub(

)

dev.c

#include

"log.h"

void

sub(

)

mul.c

#include

"log.h"

void

sub(

)

log.h

#include

static

int a =10;

static

int b =10;

void

sum();

void

dev();

void

mul();

void

sub(

);

result : main.o sum.o sub.o dev.o mul.o

gcc -o result main.o sum.o sub.o dev.o mul.o

main.o : main.c log.h

gcc -c main.c

sum.o : sum.c log.h

gcc -c sum.c

sub.o :sub.c log.h

gcc -c sub.c

dev.o : dev.c log.h

gcc -c dev.c

mul.o : mul.c log.h

gcc -c mul.c

.phony:clean

clean:

rm result main.o sum.o sub.o dev.o mul.o

2.引入變數,使makefile更簡潔

把重複的,比較長的都換成變數

=替換 +=追加 :=恆等於

使用變數的方法 $(變數名)

從上面可以看到每乙個.o檔案都有log.h,所以就寫在一起

objects = main.o sum.o sub.o dev.o mul.o

cc = gcc

tar = result

$(tar)

: $(objects)

$(cc)

-o $(tar) $(objects)

$(objects)

: log.h

main.o : main.c

sum.o : sum.c

sub.o : sub.c

dev.o : dev.c

mul.o : mul.c

.phony:clean

clean:

rm $(tar) $(objects)

3.隱含規則和引入萬用字元

%.c:任意.c檔案

%.o:任意.o檔案

*.c:所有.c檔案

*.o:所有.o檔案

$^:所有依賴檔案

$@:所有目標檔案

@<:所有依賴檔案的第乙個檔案

objects = main.o sum.o sub.o dev.o mul.o

cc = gcc

tar = result

$(tar)

: $(objects)

$(cc)

-o $@ $^

$(objects)

: log.h

%.o :%.c

.phony:clean

clean:

rm $(tar) $(objects)

不太完整有待補充。

乙個最簡單的Makefile例子

1.hello.c include int main 2.makefile hello hello.o cc o hello hello.o hello.o hello.c cc c hello.c clean rm hello.o 說明 cc o hello hello.o前面是乙個tab的空格 ...

編譯 1 第乙個makefile簡單例子

前言 本篇用乙個最簡單的例子引入makefile,教你編寫第乙個makefile 正文 在download aa資料夾下有a.c和makefile檔案 1 litao litao downloads aa ls 2 a.c makefile 其中a.c為 1 include2 intmain 311...

編譯 1 第乙個makefile簡單例子

前言 本篇用乙個最簡單的例子引入makefile,教你編寫第乙個makefile 正文 在download aa資料夾下有a.c和makefile檔案 1 litao litao downloads aa ls 2 a.c makefile 其中a.c為 1 include2 intmain 311...