zoukankan      html  css  js  c++  java
  • Makefile应用之Complicated工程

    参考《专业嵌入式软件开发》中Makefile的complicated工程代码。

    工程目录结构如下:

    .
    ├── define.h
    ├── foo.c
    ├── foo.h
    ├── main.c
    └── Makefile

    1.Makefile

    MKDIR := mkdir
    RM := rm
    RMFLAGS := -fr
    CC := gcc
    
    
    DIR_OBJS = objs
    DIR_TARGET = exes
    DIR_DEPS = deps
    TARGET = complicated
    
    DIRS = $(DIR_OBJS) $(DIR_TARGET) $(DIR_DEPS)
    SRCS = $(wildcard *.c)
    
    TARGET := $(addprefix $(DIR_TARGET)/,$(TARGET))
    
    #OBJS = $(SRCS:.c=.o)
    OBJS = $(patsubst %.c,%.o,$(SRCS))
    OBJS := $(addprefix $(DIR_OBJS)/,$(OBJS))
    
    DEPS = $(patsubst %.c,%.dep,$(SRCS))
    DEPS := $(addprefix $(DIR_DEPS)/,$(DEPS))
    
    ifeq ($(wildcard $(DIR_OBJS)),)
    DEP_DIR_OBJS := $(DIR_OBJS)
    endif
    ifeq ($(wildcard $(DIR_TARGET)),)
    DEP_DIR_TARGET := $(DIR_TARGET)
    endif
    ifeq ($(wildcard $(DIR_DEPS)),)
    DEP_DIR_DEPS := $(DIR_DEPS)
    endif
    
    all:$(TARGET)
    
    # 把依赖文件包含进来
    ifneq ($(MAKECMDGOALS),clean)
    -include $(DEPS)
    endif
    $(DIRS):
        @echo "Making $@"
        $(MKDIR) $@
    
    
    $(TARGET):$(DEP_DIR_TARGET) $(OBJS) 
        @echo "DEPS is $(DEPS)"
        @echo "making $@"
        $(CC) -o $@ $(filter %.o,$^)
    
    $(DIR_DEPS)/%.dep:$(DEP_DIR_DEPS) %.c  
        @echo "Making $@"    
        $(CC) -MM -MT '$(addprefix $(DIR_OBJS)/,$(patsubst %.c,%.o,$<)) $@' -MF $@ $(filter %.c,$^)
    
    
    $(DIR_OBJS)/%.o:$(DEP_DIR_OBJS) %.c
        @echo "making $@"
        $(CC) -c -o $@ $(filter %.c,$^)
    
    
    clean:
        $(RM) $(RMFLAGS)  $(DIRS) 
    .PHONY: all clean

    2.main.c

    #include <stdio.h>
    void foo();
    int main()
    {
        printf("This is main()!
    ");
    
        foo();
    
        return 0;
        
    }

    3.foo.c

    #include <stdio.h>
    #include "foo.h"
    
    void foo()
    {
        printf("This is foo()!MYFOO is %d
    ",MYFOO);
    
    }

    4.foo.h

    #ifndef __FOO_H
    #define __FOO_H
    #include "define.h"
    //#define   MYFOO          160
    void foo();
    
    #endif

    5.define.h

    #ifndef __DEFINE_H
    #define __DEFINE_H
    
    
    #define MYFOO    120
    
    #endif
  • 相关阅读:
    6种基本排序(C++实现)
    关于 ^ 异或 及 无中间变量进行交换
    清理C盘旧驱动
    sqlmap基本使用
    http头部注入
    waf绕过注入
    mysql报错注入
    Burp Suite工具使用
    mysql注入
    Linux网络配置
  • 原文地址:https://www.cnblogs.com/yangjiguang/p/11628683.html
Copyright © 2011-2022 走看看