• 【makefile】02 经典应用示例


    经典应用示例

    1. 文件结构:

    .
    ├── common
    │   └── common.h
    ├── dir1
    │   ├── a1.c
    │   ├── a1.h
    │   ├── b1.c
    │   ├── b1.h
    │   ├── c1.c
    │   ├── c1.h
    │   └── dir1.mk
    ├── dir2
    │   ├── a2.c
    │   ├── a2.h
    │   ├── b2.c
    │   ├── b2.h
    │   ├── c2.c
    │   ├── c2.h
    │   └── dir2.mk
    ├── dir3
    │   ├── a3.c
    │   ├── a3.h
    │   ├── b3.c
    │   ├── b3.h
    │   ├── c3.c
    │   ├── c3.h
    │   └── dir3.mk
    ├── LICENSE
    ├── makefile
    ├── README.md
    └── sys-make
        ├── config
        │   ├── build.config
        │   └── version.config
        ├── gcc.mk
        ├── rules.mk
        └── tools.mk
    
    6 directories, 30 files

    2. makefile文件:

    a. dir1.mk

    # Sub-folder makefile
    #
    #    @File:      dir1.mk
    #    @Author:    How.Chen
    #    @History:
    #    VER        DATE            Change
    #    1.0        27/May/2017        init version
    #    1.1        12/Oct/2020        update suffix
    
    # define module name for compile use
    MODULE = dir1
    
    # define expected lib
    MOD_LIB = $(LIB_PREFIX)$(MODULE).$(LIB_SUFFIX)
    
    
    CC_DEFS :=
    # modify sys-make/config/build.config to control 
    ifeq '$(filter FEATURE_1, $(FEATURE))' 'FEATURE_1'
        CC_DEFS += dir1_DEF_1
        CC_DEFS += dir1_DEF_2
        CC_DEFS += dir1_DEF_3
    endif
    
    # add srouce files, which would like to compile
    SRC_FILES =
    SRC_FILES += a1.c
    SRC_FILES += b1.c
    # modify sys-make/config/build.config to control 
    ifeq '$(filter FEATURE_1, $(FEATURE))' 'FEATURE_1'
        SRC_FILES += c1.c
    endif
    
    # add include search path
    INC_PATH =
    INC_PATH += $(TOP_DIR)/common
    
    # add source file search path together with vpath
    SRC_PATH =
    SRC_PATH += $(TOP_DIR)/$(MODULE)
    SRC_PATH += $(TOP_DIR)/$(MODULE)/sub_dir1
    
    vpath %.c $(SRC_PATH)
    
    # use general tools setting, compiler and compile rules
    include $(MKFILE_DIR)/tools.mk
    include $(MKFILE_DIR)/gcc.mk
    include $(MKFILE_DIR)/rules.mk

    b. dir2.mk

    MODULE = dir2
    
    MOD_LIB = $(LIB_PREFIX)$(MODULE).$(LIB_SUFFIX)
    
    CC_DEFS :=
    ifeq '$(filter FEATURE_2, $(FEATURE))' 'FEATURE_2'
        CC_DEFS += dir2_DEF_1
        CC_DEFS += dir2_DEF_2
        CC_DEFS += dir2_DEF_3
    endif
    
    SRC_FILES =
    SRC_FILES += a2.c
    SRC_FILES += b2.c
    SRC_FILES += c2.c
    
    INC_PATH =
    INC_PATH += $(TOP_DIR)/common
    
    SRC_PATH =
    SRC_PATH += $(TOP_DIR)/$(MODULE)
    SRC_PATH += $(TOP_DIR)/$(MODULE)/sub_dir2
    
    vpath %.c $(SRC_PATH)
    
    include $(MKFILE_DIR)/tools.mk
    include $(MKFILE_DIR)/gcc.mk
    include $(MKFILE_DIR)/rules.mk

    c. dir3.mk

    MODULE = dir3
    
    MOD_LIB = $(LIB_PREFIX)$(MODULE).$(LIB_SUFFIX)
    
    CC_DEFS :=
    ifeq '$(filter FEATURE_3, $(FEATURE))' 'FEATURE_3'
        CC_DEFS += dir3_DEF_1
        CC_DEFS += dir3_DEF_2
        CC_DEFS += dir3_DEF_3
    endif
    
    SRC_FILES =
    SRC_FILES += a3.c
    SRC_FILES += b3.c
    SRC_FILES += c3.c
    
    INC_PATH =
    INC_PATH += $(TOP_DIR)/common
    
    SRC_PATH =
    SRC_PATH += $(TOP_DIR)/$(MODULE)
    SRC_PATH += $(TOP_DIR)/$(MODULE)/sub_dir3
    
    vpath %.c $(SRC_PATH)
    
    include $(MKFILE_DIR)/tools.mk
    include $(MKFILE_DIR)/gcc.mk
    include $(MKFILE_DIR)/rules.mk

    d. makefile

    # main makefile
    #
    #    @File:      makefile
    #    @Author:    How.Chen
    #    @History:
    #    VER        DATE            Change
    #    1.0        27/Apr/2017        init commit
    #    1.1        12/Oct/2020        change suffix name andadd tools.mk
    
    # define useful directory path
    TOP_DIR = $(PWD)
    MKFILE_DIR = $(TOP_DIR)/sys-make
    CFG_DIR = $(MKFILE_DIR)/config
    OUTPUT_DIR = $(TOP_DIR)/output
    
    # define useful prefix/postfix
    LIB_PREFIX = lib
    LIB_SUFFIX = a
    
    # export var, which need be known by sub-makefile
    export TOP_DIR MKFILE_DIR OUTPUT_DIR
    export LIB_PREFIX LIB_SUFFIX
    
    all: obj link
    
    obj:
        @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk
        @$(MAKE) -f $(TOP_DIR)/dir2/dir2.mk
        @$(MAKE) -f $(TOP_DIR)/dir3/dir3.mk
    
    # link workaround
    # pass link to rules.mk to trigger link
    link:
        @$(MAKE) -f $(MKFILE_DIR)/rules.mk link
    
    # check
    # to display each module build info
    check:
        @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk check
        @$(MAKE) -f $(TOP_DIR)/dir2/dir2.mk check
        @$(MAKE) -f $(TOP_DIR)/dir3/dir3.mk check
    
    # remove ouyput
    clean:
        @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk clean
        @$(MAKE) -f $(TOP_DIR)/dir2/dir2.mk clean
        @$(MAKE) -f $(TOP_DIR)/dir3/dir3.mk clean
        -$(RM) -rf $(OUTPUT_DIR)
    
    # include build configuration
    # FEATURE define in it
    include $(MKFILE_DIR)/tools.mk
    include $(CFG_DIR)/build.config
    include $(CFG_DIR)/version.config

    3. 执行:

    [root@centos8 makefile]# make
    make[1]: Entering directory '/root/makefile'
    CC: a1.c
    CC: b1.c
    CC: c1.c
    Generate lib: libdir1.a
    make[1]: Leaving directory '/root/makefile'
    make[1]: Entering directory '/root/makefile'
    CC: a2.c
    CC: b2.c
    CC: c2.c
    Generate lib: libdir2.a
    make[1]: Leaving directory '/root/makefile'
    make[1]: Entering directory '/root/makefile'
    CC: a3.c
    CC: b3.c
    CC: c3.c
    Generate lib: libdir3.a
    make[1]: Leaving directory '/root/makefile'
    make[1]: Entering directory '/root/makefile'
    Link executable: test_makefile_2021.42.3.23.02
    full objs: /root/makefile/output/dir2/b2.o /root/makefile/output/dir2/a2.o /root/makefile/output/dir2/c2.o /root/makefile/output/dir3/b3.o /root/makefile/output/dir3/c3.o /root/makefile/output/dir3/a3.o /root/makefile/output/dir1/b1.o /root/makefile/output/dir1/a1.o /root/makefile/output/dir1/c1.o
    make[1]: Leaving directory '/root/makefile'

     

    参考资料

    1. makefile【howhow】

    2. 跟我一起学makefile

  • 相关阅读:
    上海电信 华为HG8240R 光猫 破解
    RedSn0w 0.9.10b5 越狱iphone 4 ios 5.0.1 Cydia闪退解决
    用IIS 7.5 Express代替IIS和开发工具vs自带的ASP.NET Development Server
    远程桌面连接问题
    Enterprise Library 5.0 Hands On Lab(1):数据访问程序块(一)
    [Havok] Havok Physics物理引擎的学习入门
    [设计模式] 深入浅出单实例Singleton设计模式(Java版)
    [C#] MD5 加密的具体流程
    [轻音乐] 理查德·克莱德曼专辑[8CD]
    [SEO] [DeDe]优化SEO
  • 原文地址:https://www.cnblogs.com/sunbines/p/15431301.html
Copyright © 2020-2023  润新知