多级目录中的Makefile

以下示例演示了如何在多级目录下维护多个Makefile,以使每级目录都可单独支持'make'命令。

 

目录结构:

[user@localhost maketest]$ tree

.

|-- Makefile --- Makefile 1

|-- subdir1

|   `-- Makefile --- Makefile 2

`-- subdir2

    |-- Makefile --- Makefile 3

    `-- test.cpp

 

Makefile 1:

SUBDIRS = subdir1 subdir2

 

.PHONY: default all clean $(SUBDIRS)

 

default: all

 

all clean:

#   this 'make' cmd will cause the Makefile be read again by 'make'

    $(MAKE) $(SUBDIRS) TARGET=$@

 

$(SUBDIRS): print

    $(MAKE) -C $@ $(TARGET)

 

print:

    @echo "--- MAKE  =$(MAKE)"

    @echo "--- TARGET=$(TARGET)"

 

Makefile 2:

.PHONY: all clean

 

all:

    @echo "----- make all in subdir1"

    @echo "----- current dir is: $(PWD)"

    @echo "----- current time is: $(shell date)"

 

clean:

    @echo "----- make clean in subdir1"

 

Makefile 3:

CXX     = g++

BIN     = test.exe

OBJS    = test.o

LDLIBS += -lstdc++

 

.PHONY: all clean print_all

 

all: print_all $(BIN)

 

print_all:

    @echo "----- make all in subdir2 -----"

 

$(BIN): $(OBJS)

    $(CXX) $(OBJS) -o $@ $(LDLIBS)

 

%.o: %.cpp

    $(CXX) -c $<

 

clean:

    @echo "----- make clean in subdir2 -----"

    rm -f $(BIN) $(OBJS)

 

test.cpp:

#include <iostream>

 

using namespace std;

 

int main(void)

{

    cout << __FILE__ << "," << __LINE__ << endl;

 

    return 0;

}

 

Output of ‘make’

[user@localhost maketest]$ make

make subdir1 subdir2 TARGET=all

make[1]: Entering directory `/home/user/maketest'

--- MAKE  =make

--- TARGET=all

make -C subdir1 all

make[2]: Entering directory `/home/user/maketest/subdir1'

----- make all in subdir1

----- current dir is: /home/user/maketest

----- current time is: Sun Mar 13 16:14:43 CST 2011

make[2]: Leaving directory `/home/user/maketest/subdir1'

make -C subdir2 all

make[2]: Entering directory `/home/user/maketest/subdir2'

----- make all in subdir2 -----

g++ -c test.cpp

g++ test.o -o test.exe -lstdc++

make[2]: Leaving directory `/home/user/maketest/subdir2'

make[1]: Leaving directory `/home/user/maketest'

 

Output of ‘make clean’:

[user@localhost maketest]$ make clean

make subdir1 subdir2 TARGET=clean

make[1]: Entering directory `/home/user/maketest'

--- MAKE  =make

--- TARGET=clean

make -C subdir1 clean

make[2]: Entering directory `/home/user/maketest/subdir1'

----- make clean in subdir1

make[2]: Leaving directory `/home/user/maketest/subdir1'

make -C subdir2 clean

make[2]: Entering directory `/home/user/maketest/subdir2'

----- make clean in subdir2 -----

rm -f test.exe test.o

make[2]: Leaving directory `/home/user/maketest/subdir2'

make[1]: Leaving directory `/home/user/maketest'

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值