makefile的几种写法【原创】

Makefile  编写

 

Make  -f  makefile1 指定

如下是 本人的一点makefile学习笔记,再分享一个不错的写makefile总结的网址:

http://www.cnblogs.com/wang_yb/p/3990952.html

 

方式一、 同一个目录下有多个.c .h文件 CROSS_COMPILE=arm-none-linux-gnueabi-gcc #CC=arm-none-linux-gnueabi-gcc TEST_RES := regDebug objects = main.o uart.o #下面这行表示依赖关系 表示所有的.o文件都是依赖于.c # $@是目标%.o %.o:%.c arm-none-linux-gnueabi-gcc -c $< -o $@ $(TEST_RES): $(objects) #$(CC) $^ -o $@ $(CROSS_COMPILE) $^ -o $@ .PHONY: clean clean: rm -rf .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) 方式二、 同一个目录下有多个.c .h文件 CC=arm-none-linux-gnueabi-gcc TEST_RES := regDebug objects = main.o uart.o $(TEST_RES): $(objects) $(CC) $^ -o $@ .PHONY: clean clean: rm -rf .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) 方法三、 将编译生成的中间文件放在obj中,编译生成的可执行文件放在bin目录下 TEST_RES := bin/regDebug objects = ./obj/main.o \ ./obj/uart.o ./obj/%.o:%.c $(CC) -c $< -o $@ $(TEST_RES): $(objects) $(CC) $^ -o $@ .PHONY: clean clean: rm -rf  .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) rm -rf $(objects) 方法四、 用OBJDIR替换了路径 OBJDIR=obj TEST_RES := bin/regDebug objects = $(OBJDIR)/main.o \ $(OBJDIR)/uart.o $(OBJDIR)/%.o:%.c $(CC) -c $< -o $@ $(TEST_RES): $(objects) $(CC) $^ -o $@ .PHONY: clean clean: rm -rf  .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) rm -rf $(objects) 方法五、 将uart.c和uart.h放在package目录下 CC=arm-none-linux-gnueabi-gcc INCLUDDIR=-I package OBJDIR=obj TEST_RES := bin/regDebug objects = $(OBJDIR)/main.o \ $(OBJDIR)/uart.o $(OBJDIR)/%.o:%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(OBJDIR)/%.o:./package/%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(TEST_RES): $(objects) $(CC) $^ -o $@ .PHONY: clean clean: rm -rf  .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) rm -rf $(objects) 方法六、 CC=arm-none-linux-gnueabi-gcc INCLUDDIR=-I package OBJDIR=obj TEST_RES := bin/regDebug OBJECTS = $(OBJDIR)/main.o -include ./package/obj.mk $(OBJDIR)/%.o:%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(OBJDIR)/%.o:./package/%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(TEST_RES): $(OBJECTS) $(CC) $^ -o $@ .PHONY: clean clean: rm -rf  .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) rm -rf $(OBJECTS)  obj.mk OBJECTS += ./obj/uart.o 方法七、 把所有头文件都放在include目录下 CC=arm-none-linux-gnueabi-gcc INCLUDDIR=-I include OBJDIR=obj TEST_RES := bin/regDebug OBJECTS = $(OBJDIR)/main.o -include ./package/obj.mk $(OBJDIR)/%.o:%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(OBJDIR)/%.o:./package/%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(TEST_RES): $(OBJECTS) $(CC) $^ -o $@ .PHONY: clean clean: rm -rf  .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) rm -rf $(OBJECTS) 方法八、 makefile的几种写法【原创】 #CC=arm-none-linux-gnueabi-gcc #INCLUDDIR=-I include #OBJDIR=obj -include rules.mk TEST_RES := bin/regDebug OBJECTS = $(OBJDIR)/main.o -include ./package/obj.mk $(OBJDIR)/%.o:%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(OBJDIR)/%.o:./package/%.c $(CC) $(INCLUDDIR) -c $< -o $@ $(TEST_RES): $(OBJECTS) $(CC) $^ -o $@ .PHONY: clean clean: rm -rf  .*.cmd *.o *.mod.c .tmp_versions Module.symvers .Makefile.swp $(TEST_RES) rm -rf $(OBJECTS) rules.mk文件中 CC=arm-none-linux-gnueabi-gcc INCLUDDIR=-I include OBJDIR=obj

方法九、

目录结构如下

bin下为可执行文件

include 下为头文件

obj 下为编译的中间文件

package下为各种c文件

 makefile的几种写法【原创】

 

根目录下makefile

-include rules.mk

 

TEST_RES := bin/regDebug

 

OBJECTS = $(OBJDIR)/main.o

 

-include ./package/obj.mk

 

$(OBJDIR)/%.o:%.c

        $(CC) $(INCLUDDIR) -c $< -o $@ -std=gnu99

 

$(OBJDIR)/%.o:./package/%.c

        $(CC) $(INCLUDDIR) -c $< -o $@ -std=gnu99

 

 

$(TEST_RES): $(OBJECTS)

        $(CC) $^ -o $@ -lpthread -std=gnu99

        chmod a+x $@

       

.PHONY: clean

 

clean:

        rm -rf  .*.cmd *.o *.mod.c  .tmp_versions Module.symvers .Makefile.swp $(TEST_RES)

        rm -rf  $(OBJECTS)

 

 

根目录下 rules.mk:

 

CC=arm-none-linux-gnueabi-gcc

 

INCLUDDIR=-I include

OBJDIR=obj

 

package目录下 obj.mk

OBJECTS += ./obj/uart.o \

                    ./obj/Console.o \

                    ./obj/reg_data.o \

                    ./obj/func.o \

                    ./obj/run.o \

                    ./obj/revDataOne.o

 

 

 如有转载请注明出处

新浪博客:http://blog.sina.com.cn/u/2049150530
博客园:http://www.cnblogs.com/sky-heaven/
知乎:http://www.zhihu.com/people/zhang-bing-hua

 

你可能感兴趣的:(makefile)