多应用程序编译,Makefile

本文详细解析了Makefile文件的构建流程,包括其核心内容、关键信息以及信息技术领域的相关关键词。通过理解Makefile的基本语法和用法,读者可以更好地掌握如何设置Makefile以满足不同情况下的构建需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序结构如下


                       $projectroot
                                    |
              +---------------+----------------+
              |                     |                       |
            part1/          part2/              part3/
              |                   |                          |
      +------+-----+    +---+----+      +---+-----+
       |         |       |       |            |        |              |
     data/  src/  inc/    src/     inc/     src/        inc/




参考写法:
CC=g++
TARGET=cppTest
OTHERDIR=../../someotherpath/in/project/src

SOURCE = cppTest.cpp
SOURCE = $(OTHERDIR)/file.cpp

## End sources definition
INCLUDE = -I./ $(AN_INCLUDE_DIR)  
INCLUDE = -I.$(OTHERDIR)/../inc
## end more includes

VPATH=$(OTHERDIR)
OBJ=$(join $(addsuffix ../obj/, $(dir $(SOURCE))), $(notdir $(SOURCE:.cpp=.o))) 

## Fix dependency destination to be ../.dep relative to the src dir
DEPENDS=$(join $(addsuffix ../.dep/, $(dir $(SOURCE))), $(notdir $(SOURCE:.cpp=.d)))

## Default rule executed
all: $(TARGET)
        @true

## Clean Rule
clean:
        @-rm -f $(TARGET) $(OBJ) $(DEPENDS)


## Rule for making the actual target
$(TARGET): $(OBJ)
        @echo "============="
        @echo "Linking the target $@"
        @echo "============="
        @$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
        @echo -- Link finished --

## Generic compilation rule
%.o : %.cpp
        @mkdir -p $(dir $@)
        @echo "============="
        @echo "Compiling {1}lt;"
        @$(CC) $(CFLAGS) -c {1}lt; -o $@


## Rules for object files from cpp files
## Object file for each file is put in obj directory
## one level up from the actual source directory.
../obj/%.o : %.cpp
        @mkdir -p $(dir $@)
        @echo "============="
        @echo "Compiling {1}lt;"
        @$(CC) $(CFLAGS) -c {1}lt; -o $@

# Rule for "other directory"  You will need one per "other" dir
$(OTHERDIR)/../obj/%.o : %.cpp
        @mkdir -p $(dir $@)
        @echo "============="
        @echo "Compiling {1}lt;"
        @$(CC) $(CFLAGS) -c {1}lt; -o $@

## Make dependancy rules
../.dep/%.d: %.cpp
        @mkdir -p $(dir $@)
        @echo "============="
        @echo Building dependencies file for $*.o
        @$(SHELL) -ec '$(CC) -M $(CFLAGS) {1}lt; | sed "s^$*.o^../obj/$*.o^" > $@'

## Dependency rule for "other" directory
$(OTHERDIR)/../.dep/%.d: %.cpp
        @mkdir -p $(dir $@)
        @echo "============="
        @echo Building dependencies file for $*.o
        @$(SHELL) -ec '$(CC) -M $(CFLAGS) {1}lt; | sed "s^$*.o^$(OTHERDIR)/../obj/$*.o^" > $@'

## Include the dependency files
-include $(DEPENDS)


参考Makefile语法

1.http://blog.youkuaiyun.com/yrj/article/details/4046853

2.http://locklessinc.com/articles/makefile_tricks/   【very nice】

3.http://blog.chinaunix.net/space.php?uid=11278770&do=blog&id=148571

4.The best way to set up makefiles for various situations   http://makepp.sourceforge.net/1.19/makepp_cookbook.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值