• 手把手教你写makefile【原创】


    Makefile  编写

     

    Make  -f  makefile1 指定

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

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

    Makefile  $@, $^, $< 
    $@  表示目标文件
    $^  表示所有的依赖文件
    $<  表示第一个依赖文件
    $?  表示比目标还要新的依赖文件列表
    方式一、
    
    同一个目录下有多个.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)
    
    方法八、
    
     
    
    #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

    -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

    方法十、

    obj-m := driver.o
    MAKEOPT :=
    ARCH="arm"
    CROSS_COMPILE="arm-none-linux-gnueabi-"

    ARM_LINUX_KERNEL := "/home/zhangbh/firmware/build_dir/linux-brcm5830_sxxx-prolin2/linux-3.6.5/"

    PWD = $(shell pwd)

    all:
    $(MAKE) $(MAKEOPT) -C $(ARM_LINUX_KERNEL) M=$(PWD) modules

    clean:
    rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions Module.symvers .Makefile.swp modules.order

    方法十一、

    L_DIR=-L /home/zhangbh/new/firmware/staging_dir/target-arm-none-linux-gnueabi/usr/lib
    I_DIR=-I /home/zhangbh/new/firmware/staging_dir/target-arm-none-linux-gnueabi/usr/include/

    L_FLAG=-losal
    -lcbinder
    -ljpeg
    -lfreetype
    -lpng
    -lz
    -lpthread
    -lrt
    -lcrypt
    -lssl
    -lcrypto
    -lts
    -lprop
    -lcutils
    -lxui

    VBS_t:VBS.c
    arm-none-linux-gnueabi-gcc -o $@ $< $(I_DIR) $(L_DIR) $(L_FLAG)

    方法十三、

    obj-m:=demo.o

    ARCH:=arm
    CROSS_COMPILE:=/home/zhangbh/cygnus/3.5.4-RC2/iproc/buildroot/buildroot/host/usr/bin/
    KERNEL_HEADERS:=/home/zhangbh/cygnus/3.5.4-RC2/iproc/kernel/linux-3.6.5

    build:kernel_modules
    kernel_modules:
    make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_HEADERS) M=$(CURDIR) modules

    clean:
    make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_HEADERS) M=$(CURDIR) clean

    方式十四、 驱动和应用反汇编调试 objdump

    obj-m := kmod-demo1.o
    MAKEOPT := ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
    #CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
    ARM_LINUX_KERNEL := /home/zhangbh/Prolin_os/si/Prolin/firmware_cygnus/build_dir/linux-brcm5830_sxxx-prolin2/linux-3.6.5
    ccflags-y :=-g
    PWD = $(shell pwd)

    all:
    $(MAKE) $(MAKEOPT) -C $(ARM_LINUX_KERNEL) M=$(PWD) modules

    .PHONY: clean

    clean:
    rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions Module.symvers .Makefile.swp modules.order

     

    info 可以用来在makefile中打印
    
    include $(TOPDIR)/.config 
    $(info tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt)
    $(info $(CONFIG_ENABLE_AA))
    
    
    include $(TOPDIR)/.config 
    ifeq ($(CONFIG_ENABLE_PCI),y)
    EXTRA_CFLAGS += -DCONFIG_ENABLE_PCI
    endif
     
    
     ifeq ($(CONFIG_ENABLE_PCI),y)
    
    EXTRA_CPPFLAGS += -DCONFIG_ENABLE_PCI
    endif
    
     编译的时候可以在error时停下
    
    34 #ifdef CONFIG_ENABLE_PCI
    35 #error "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    36 printk("
    
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa===============
    
    ");
    37 #endif

     如有转载请注明出处

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

     

  • 相关阅读:
    codeforces 732D
    我的ubuntu配置
    周末实在无聊,自己写了一遍被人写烂了的“贪吃蛇”,欢迎各位新手低手高手老手前来吐槽哈哈
    两种方式实现点击列表弹出列表索引
    浅谈javascript中原型(prototype)、构造函数、对象实例及三者之间的关系
    写了个超级简单的“进度条”的demo,供新手参考,高手吐槽!
    javascript 使用 NodeList主意的问题
    js中的“||”和“&&”
    javascript call函数实现继承时遇到的问题
    javascript 解决innerText浏览器兼容
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/4652804.html
Copyright © 2020-2023  润新知