Makefile Writing Essentials

Source: Internet
Author: User

#PS: Please respect the original, do not like to spray


#PS: To reprint please specify the source, I all rights reserved


#PS: This is just " myself" understanding, if it conflicts with your principles, please


Understanding, do not spray


Recently tidied up their files, found that because of too lazy reasons, a lot of information is too late to tidy up forget, it is a pity, so, in the collation of makefile, as a novice, write makefile some questions immediately write down.


1 Assigning values to variables

Varname= is the most basic assignment.
varname: = is the value before overwriting
varname= is the value after the equals sign if it is not assigned
varname+ = is the value after adding an equal sign



2 meaning of several special symbols

< Span style= "FONT-FAMILY:VERDANA,ARIAL,HELVETICA,SANS-SERIF; font-size:14px; line-height:21px "> [email protected" target file,

< Span style= "FONT-FAMILY:VERDANA,ARIAL,HELVETICA,SANS-SERIF; font-size:14px; line-height:21px "> $^--all dependent files,

< Span style= "FONT-FAMILY:VERDANA,ARIAL,HELVETICA,SANS-SERIF; font-size:14px; line-height:21px "> $<--the first dependent file.


3 Multi-Catalog Makefile Writing Essentials

With command: ${make}-C ${subdirectory} ${targetname}

Note: The above command switches to subdirectories and executes the make command

Build makefile in subdirectories and work well on dependencies


4 Makefile Partial function use

patsubst (<pattern>,< replacement>,<text>)
Function: Find words in <text> (word with "space", "tab" or "carriage return" NewLine "delimited" is  
conforms to pattern <pattern> if matched, it is replaced with <replacement>. Here,<pattern> can include   the
wildcard character "%", which represents any length of string. If <replacement> also contains "%", then  
<replacement> This "%" will be the string represented by the "%" in <pattern>. (You can use "\" &NBSP;
to escape, "\%" to represent the true meaning of the "%" character)  
Returns: The function returns the string that was replaced.

Example:
$ (patsubst%.c,%.o,tmp.c.c tmp1.c)
Replace the word string "tmp.c.c tmp1.c" with [%.c] with [%.O] and return the result "tmp.c.o tmp1.o"


$ (strip <string>)

Function: Remove the empty characters from the beginning and end of the <string> string.
Return: Returns the string value of the stripped space.
Example:
$ (Strip ABC)
The string "abc" goes to the beginning and end of the space, the result is "ABC".

${wildcard <partner>)

Function: src = $ (wildcard *.c)

Returns: searches all files in the current directory that end in. C, generates a list of file names separated by spaces, and assigns them to SRC. The current directory file has only a file name, and the file name under the subdirectory contains the path information

${notdir textlist)

Use: src = $ (notdir textlist)

Returns: Removes all directory information, the file name list in SRC will have only the filename.




Examples of my makefile:

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:d efault 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 do 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 do 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}


The pure hand dozen, inevitably has the mistake place, welcome correct correction.

#PS: Please respect the original, do not like to spray


#PS: To reprint please specify the source, I all rights reserved


If you have any questions, please leave a message and I will reply when I see it.


Makefile Writing Essentials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.