这里对linux下的几种可能会经常用到的Makefile文件写法做一个备份,方便以后要写Makefile时使用:
1、应用层驱动的Makefile文件:
# build a Makefile
# 希望生成的目标
EXE = Targer_xxx
# 用 objects_o 这个变量表示全部 .o文件
# 分别对应111.c 222.c ...
objects_o = 111.o 222.o
# 把中间文件(.o文件)合成执行文件 (-o)表示合成执行文件
# 变量需要用“$()”$符号来表示
$(EXE) : $(objects_o)
$(CC) $(LDFLAGS) $(objects_o) -o $(EXE) -lm
# 定义编译规则
# 将本目录下所有的 c 程序编译成汇编代码(.o文件)
%.s: %.c
$(CC) $(CFLAGS) -S $< -o $@
# 这一段和上面的一段的作用是一样的,可能在某些情况下会用到,就写出来了。
# 生成xxx.o文件 (-c)表示生成.o文件
# 命令行用tab键开头 也可以用“;”写成一行
#111.o: 111.c 111.h
# $(CC) $(CFLAGS) -c 111.c # 本行Makfile的规则是可以自动推导出来的,可以省略
#222.o: 222.c 222.h
# $(CC) $(CFLAGS) -c 222.c
# .PHONY表示clean是个伪目标 可以不加,添加更稳妥
# -rm的“-”:也许某些文件出现问题,但不要管,继续做后面的事. 也可以不加
# 顺便提一句,在make的时候发现编译错误,有可能就是编译应用层的驱动没有make clean
.PHONY: clean
clean:
rm *.o $(EXE)
2、package:生成.ipk包的Makefile:
###############################################
#OpenWrt Makefile
##############################################
include $(TOPDIR)/rules.mk
object_device = Targer_xxx
# Name and release number of this package
PKG_NAME:=$(object_device)
PKG_RELEASE:=2017.3.19
# This specifies the directory where we're going to build the program.
# The root build directory, $(BUILD_DIR), is by default the build_mipsel
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/$(object_device)
SECTION:=utils
CATEGORY:=Utilities
TITLE:=$(object_device) function -- Use the $(object_device) module
endef
# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/$(object_device)/description
$(object_device) function Use the $(object_device) module.
endef
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default. The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one
# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist. Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/$(object_device)/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(object_device) $(1)/bin/
endef
# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to build a package.
$(eval $(call BuildPackage,$(object_device)))
3、….:
本文提供Linux环境下两种常见Makefile的编写示例:一是针对应用层驱动的Makefile,详细介绍了目标文件、编译规则等内容;二是生成.ipk包的Makefile,适用于OpenWrt环境。
4981

被折叠的 条评论
为什么被折叠?



