项目中Makefile写法的一种参考

项目文件目录

  • 项目目录
    • include
      • main
        • main.h
      • myclass
        • myclass.h
      • tools
        • tools.h
    • lib
    • src
      • main
        • main.c
        • Makefile
      • myclass
        • myclass.c
        • Makefile
      • tools
        • tools.c
        • Makefile
    • Makefile

项目总Makefile文件编写

# Top Makefile for C program
# Copyright (C) 2021 by SL:sunlin0322@foxmail.com
# We are using a recursive build, so we need to do a little thinking
# to get the ordering right.
#
# Most importantly: sub-Makefiles should only ever modify files in
# their own directory. If in some directory we have a dependency on
# a file in another dir (which doesn't happen often, but it's often
# unavoidable when linking the built-in.o targets which finally
# turn into vmlinux), we will call a sub make in that other dir, and
# after that we are sure that everything which is in that other dir
# is now up to date.
#
# The only cases where we need to modify files which have global
# effects are thus separated out and done before the recursive
# descending is started. They are now explicitly listed as the
# prepare rule.

# Beautify output
# ---------------------------------------------------------------------------
#
# Normally, we echo the whole command before executing it. By making
# that echo $($(quiet)$(cmd)), we now have the possibility to set
# $(quiet) to choose other forms of output instead, e.g.
#
#         quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
#         cmd_cc_o_c       = $(CC) $(c_flags) -c -o $@ $<
#
# If $(quiet) is empty, the whole command will be printed.
# If it is set to "quiet_", only the short version will be printed.
# If it is set to "silent_", nothing will be printed at all, since
# the variable $(silent_cmd_cc_o_c) doesn't exist.
#
# A simple variant is to prefix commands with $(Q) - that's useful
# for commands that shall be hidden in non-verbose mode.
#
#	$(Q)ln $@ :<
#
# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
# If KBUILD_VERBOSE equals 1 then the above command is displayed.
#
# To put more focus on warnings, be less verbose as default
# Use 'make V=1' to see the full commands

#KBUILD_VERBOSE = 1

ifndef KBUILD_VERBOSE
  KBUILD_VERBOSE = 0
endif

ifeq ($(KBUILD_VERBOSE),1)
  quiet =
  Q =
else
  quiet=quiet_
  Q = @
endif
export quiet Q KBUILD_VERBOSE
#----------------------------------------------------------------
CROSS_COMPILE ?=
AS		= $(CROSS_COMPILE)as
LD		= $(CROSS_COMPILE)ld
CC		= $(CROSS_COMPILE)gcc
CPP		= $(CC) -E
AR		= $(CROSS_COMPILE)ar
NM		= $(CROSS_COMPILE)nm
NM		= -rm -f

STRIP		= $(CROSS_COMPILE)strip
OBJCOPY		= $(CROSS_COMPILE)objcopy
OBJDUMP		= $(CROSS_COMPILE)objdump

export AS LD CC CPP AR NM RM
export STRIP OBJCOPY OBJDUMP

#Define the directory of the master makefile
TOPDIR := $(shell pwd)
LIBDIR := lib
export TOPDIR

LDFLAGS := -L ./$(LIBDIR)
CFLAGS := -I $(TOPDIR)/include/main

export CFLAGS LDFLAGS

EXTRA_CFLAGS :=

#Define Target file
TARGET := test

src_files := $(filter-out main,$(shell ls ./src))
lib_files := $(addprefix -l,$(src_files))

PHONY :=

all : $(src_files) $(TARGET)
	$(Q)echo $(TARGET) has been built!

PHONY += all

$(src_files):
	$(Q)make -C $(TOPDIR)/src/$@

PHONY += $(src_files)

$(TARGET) : $(src_files)
	$(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -o $(TARGET) $(TOPDIR)/src/main/main.c $(lib_files) $(LDFLAGS)
help:
	@echo "===============A common Makefilefor c programs=============="
	@echo "Copyright (C) 2021 emall:sunlin0322@foxmail.com"
	@echo "The following targets aresupport:"
	@echo
	@echo " all              - (==make) compile and link"
	@echo " clean            - clean target"
	@echo " distclean        - clean target and otherinformation"
	@echo " help             - print help information"
	@echo
	@echo "========================= Version2.0 ======================="

PHONY += help

clean:
	$(Q)$(RM) $(shell find -name "*.o")
	$(Q)$(RM) $(TARGET)

distclean:
	$(Q)$(RM) $(shell find -name "*.o")
	$(Q)$(RM) $(shell find -name "*.d")
	$(Q)$(RM) $(shell find -name "*.a")
	$(Q)$(RM) $(TARGET)
	
PHONY += clean distclean
	
.PHONY : $(PHONY)

源文件Makefle文件编写

现以源码src文件下main文件中的Makefile为例。

CURDIR := $(shell pwd)
LIBDIR := $(TOPDIR)/lib

src_files := $(wildcard *.c)
dep_files := $(src_files:.c=.d)
obj_files := $(src_files:.c=.o)
base_name := $(shell basename $(CURDIR))
STATIC_NAME := $(base_name).a

#Define compilation options
CFLAGS := -Wall -O2 -g
#Specify header file directory
CFLAGS += -I $(TOPDIR)/include/$(base_name)
EXTRA_CFLAGS :=

PHONY :=

vpath %.h $(TOPDIR)/include/$(base_name)

all : static_library#shared_library

PHONY += all

ifneq ($(dep_files),)
include $(dep_files)
endif

static_library : $(obj_files)
	$(Q)$(AR) -cr $(LIBDIR)/lib$(STATIC_NAME) $^

PHONY += static_library

#shared_library:
#	$(Q)$(CC) -shared -fpic -o $(LIBDIR)/$(SHARE_NAME) *.c;

PHONY += install

%.o : %.c %.d
	$(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $< $(LDFLAGS)

%.d : %.c
	$(Q)set -e; rm -f $@; \
	$(CC) -MM $(CFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

clean:
	$(Q)$(RM) *.o *.d
	$(Q)$(AR) $(LIBDIR)/lib$(STATIC_NAME)

PHONY += clean
.PHONY := $(PHONY)

总结于2021年09月04日

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值