Note: Please indicate the source if reprinted, all rights reserved.Note: This is just based on my own understanding,if it conflicts with your principles and ideas, please forgive me and do not criticize.
Environment Description
None
Introduction
Recently, while organizing my files, I found that due to my laziness, I forgot a lot of information that I didn’t have time to organize, which is a pity. Therefore, while organizing the Makefile, I wrote down some questions I had as a beginner immediately.
Key Points for Writing
1 Variable Assignment
- • varname= is the most basic assignment
- • varname:= overwrites the previous value
- • varname?= assigns the value after the equal sign if it has not been assigned before
- • varname+= appends the value after the equal sign
2 Meaning of Several Special Symbols
- • $@ — target file,
- • $^ — all dependency files,
- • $< — the first dependency file.
3 Key Points for Multi-directory Makefile Writing
- • Use command: ${MAKE} -C ${subdirectory} ${TARGETNAME}
- • Note: The above command will switch to the subdirectory and execute the make command
- • Create a Makefile in the subdirectory and manage dependencies
4 Usage of Some Makefile Functions
- 1. patsubst(<pattern>,<replacement>,<text>) Function: Searches for words in <text> (words are separated by spaces, tabs, or newlines) that match the pattern <pattern>, and if matched, replaces them with <replacement>. Here, <pattern> can include the wildcard “%”, which represents a substring of any length. If <replacement> also contains “%”, then this “%” in <replacement> will represent the substring that the “%” in <pattern> stands for. (You can escape it with “\” to represent the real meaning of the “%” character)
Returns: The function returns the string after replacement.
Example:
$(patsubst %.c,%.o,tmp.c.c tmp1.c)
Replace words in the string "tmp.c.c tmp1.c" that match the pattern [%.c] with [%.o], the result is "tmp.c.o tmp1.o"
- 2. $(strip <string> )
Function: Removes leading and trailing whitespace characters from <string>.Returns: The string value with spaces removed.
Example:
$(strip abc )
Remove leading and trailing spaces from the string " abc ", the result is "abc".
- 3. ${wildcard <pattern>}
Function: src = $(wildcard *.c )
Returns: Searches for all files ending with .c in the current directory, generates a space-separated list of filenames, and assigns it to SRC. The current directory files only have filenames, while the filenames in subdirectories include path information.
- 4. ${notdir textlist}
Usage: src = $(notdir textlist)
Returns: Removes all directory information, the list of filenames in SRC will only contain filenames.
My Makefile Example
My Makefile Example:
topdir-makefile:
##############################
# file: Makefile
# author: sky
# modified-date: 2016-05-07
###############################
export ROOT_DIR := $(shell pwd)
#get out of start and end char' ' of the string
ROOT_DIR :=$(strip ${ROOT_DIR})
export LIB_DIR:=$(ROOT_DIR)/lib
export SRC_DIR:=$(ROOT_DIR)/src
export INCLUDE_DIR:=$(ROOT_DIR)/include
export TARGET:=test
export CC:=gcc
export LD_FLAGS:=-l config -pthread
export SRC:=$(wildcard ${SRC_DIR}/*.c)
export OBJ:=$(patsubst %.c,%.o,${SRC})
export OBJ_S:=Y_Start.o Y_ChildProcess.o
#if you want to build release-program , use command: make BUILD_RELEASE=TRUE
ifeq ($(BUILD_RELEASE), TRUE)
export C_FLAGS:= -I ${INCLUDE_DIR} -std=c99
export BUILD_DIR := $(ROOT_DIR)/release
else
export C_FLAGS:= -g -D Y_DEBUG -I ${INCLUDE_DIR} -std=c99
export BUILD_DIR := $(ROOT_DIR)/debug
endif
export OLD_OBJ:=$(wildcard ${BUILD_DIR}/*.o)
.PHONY :default all clean
default:all
all :
@${MAKE} -C src all
clean:
@${MAKE} -C src clean
sub-dir-makefile:
.PHONY:all clean
all:${TARGET}
@${CC} ${OBJ} -o ${TARGET} ${LD_FLAGS}
@mv ${TARGET} ${BUILD_DIR}
@mv ${OBJ} ${BUILD_DIR}
#this is to make test from a static-lib
test_static:${OBJ}
@${CC} ${C_FLAGS} ${OBJ_S} -o test_static -static -L ${LIB_DIR} -l Y_Stdio
#this is to make test from a shared-lib
test_share:${OBJ}
@${CC} ${C_FLAGS} ${OBJ_S} -o test_share -L ${LIB_DIR} -l Y_Stdio
#this is to make a static-lib
libY_Stdio_Static:
@${CC} ${C_FLAGS} -c Y_Stdio.c
@ar -rcs libY_Stdio.a Y_Stdio.o
@mv libY_Stdio.a ${LIB_DIR}
#this is to make a shared-lib
libY_Stdio_Shared:
@${CC} ${C_FLAGS} -fPIC -c Y_Stdio.c
@${CC} -shared -fPIC -o libY_Stdio.so Y_Stdio.o
@mv libY_Stdio.so ${LIB_DIR}
${TARGET}:
@${CC} ${C_FLAGS} -c ${SRC}
#clean target
clean:
@rm ${OLD_OBJ} ${BUILD_DIR}/${TARGET}
Typed manually, there may be errors, corrections are welcome.
Postscript
None.
References
None.
Donations, subscriptions, favorites, throwing bananas, coins, please follow the public account
Note: Please respect original work, if you dislike it, do not criticize.Note: Please indicate the source if reprinted, all rights reserved.Note: If you have questions, please leave a message, I will reply as soon as I see it.