Automatically manage a project of Makefile (C language)

Linux is all embedded software engineers around the past Hom,

makefile is not around the past Hom Linux system.

It took a few days to a preliminary study and understanding of the role and function of the makefile, and make a common type of makefile management follow-up possible on Linux for the independent development of the project.

In this manner a note by note. Afraid that they later forgot.

 

makefile thought: makefile core formula     

<target_file> : <source_file>

  command....

If the target file does not exist or the source file has than the target file modification date is newer, then execute command command;

Function command command is used to generate the target file Update

At the same time the source files on a formula has become the target file can be nested under a formula. Again and again.

The above formula, if the target and source files are up to date, then the command command will not be executed. A significant reduction of so many large-scale projects, will be updated only modified files and the final output file compile time .

 

At the same time the makefile supports a rich logic, transform. In-depth study can be developed as a new programming language.

The following makefile share their optimization of a document prepared by the network with the power of large cattle.

This makefile in the root directory of the project.

  For example: Pro folder there Inc, Res, Img folder;

        There are header files, source code files, picture files, project files, and so on;

        All documents on Pro projects are under Pro file, makefile file should be placed in the Pro folder

Note: Get the absolute path feature is available only in the compiler GUN

Features: Pro will file all * .c * .h file as a project, the overall compile, link;

     Recursive scan each folder

     Only after compiling uncompiled files or update an associated file

     clean command to delete all files will be generated makefile

       cleanO command (uppercase letter O) to delete the file in addition to the executable file, makefile generated.

     des command to print the current project as well as the absolute path absolute paths for all source files.

 

 

  1  # source file suffix for this project type
   2 ProjectType = .c
   3  
  4  # you want to generate the name of the executable file
   5 binName: = obj.out
   6  
  7  
  8  # Get the current makefile absolute path
   9 pes_parent_dir: = $ (shell pwd) / $ ($ lastword (MAKEFILE_LIST))
 10 pes_parent_dir: = $ ($ dirname the shell (pes_parent_dir))
 . 11  
12 is  # makefile jump to the first directory, subdirectory and then get all the directory
 13 is AllDirs: = $ ($ the shell CD (pes_parent_dir); -R & lt LS | grep ' ^ \ ./.*: $$ ' | awk ' {gsub ( ":", "");} Print ' .)
 14  
15  # add an absolute path
16 AllDirs: = $ (. N-the foreach, $ (AllDirs), $ (SUBST, $ (pes_parent_dir), $ (n-)))
 . 17  
18 is # accessories .c / .cpp file path
 . 19 the Sources: = $ (n-the foreach , $ (AllDirs), $ ($ wildcard (n-) / * $ (ProjectType)))
 20 is  
21 is # treated to give * .o file name extension
 22 is OBJS = $ ($ patsubst% (ProjectType),%. O, $ ( Sources))
 23  
24- # empathy get * .d file name
 25 DEPS: = $ (patsubst% $ (ProjectType),% d, $ (Sources)).
 26  
27  # static libraries need to use third-party
 28 StaticLib: =
 29  
30  # need to use third-party dynamic link library
 31 DynamicLib: =
 32 
33  # true binary output path (absolute)
 34 is the Bin: = $ (pes_parent_dir) / $ (binName)
 35  
36  #C compiler
 37 [ the CC = GCC
 38 is  
39 # C ++ compiler
 40 CXX = G ++
 41 is  
42 is # simplified rm - F
 43 is RM = -rm - F
 44 is  
45  #C language configuration parameter
 46 is the CFLAGS = -g -Wall -pedantic -std C99 = - O
 47  
48 # C ++ configuration parameters
 49 CXXFLAGS = -g -Wall -std = C11
 50  
51 is  # search path
 52INCLUDE_PATH = $ (the foreach n-, $ (AllDirs), - the I $ (n-))
 53 is  
54 is  
55 LDFLAGS =
 56 is  
57 is  # specified AllLibs ultimate goal namely: the latest the Bin
 58  AllLibs: $ (the Bin)
 59  
60  # statement label des path for observing the correct current
 61 is  .PHONY: des
 62 is  des:
 63 is          @echo OBJS =   $ (OBJS)
 64          @echo cur_makefile_path = $ (pes_parent_dir)
 65          @echo AllDirs = $ (AllDirs)
 66          @echo the Sources = $ (Sources)
 67          @echo DEPS = $ (DEPS)
 68  
69# Correspondence relationship in the present makefile to separated by spaces .c suffix are generated for a new file is intended to update all .d * the include file dependencies .c
 70  % .d:% .c
 71 is             @echo ' $ Finding <DEPENDING head File ' 
72             @ $ (the CC) -MT " $ (<:. C = .o) $ @" $ -MM (INCLUDE_PATH) $ (the CPPFLAGS) $ <> $ @ 
73 is  
74 # to include in the the * .d file, as long as any which a file is modified, then this will trigger a new rule generating * .o file
 75  % .o:% 2.d
 76          @echo the compile $ (<: D = C)
 77          $ @ (the CC) -C $ (<:. D = .c) $ (INCLUDE_PATH) $ (the CFLAGS) @ $
 78  
79 sinclude $ (the Sources: .c = 2.d)
 80  
81  $ (the Bin): $ (OBJS )
82          @echo Bulding ....
 83          @ $ (CC) $ (OBJS) $ (CFLAGS) $ (the Bin)
 84          @echo the Created File: $ (binName)
 85  
86  .PHONY: Clean
 87  Clean:
 88              @echo ' cleanup All files ' 
89              @ $ (RM) $ (OBJS) $ (deps) $ (the Bin)
 90  
91 is  .PHONY: cleanO
 92  cleanO:
 93              @echo ' clean && Dep the Obj ' 
94              @ $ (RM) $ (OBJS) $ (DEPS)
 95  # ############################################# ############################
 96# Separate <symbol representing the dependent source files (ie the colon: the left) $ < represents the source file expands to a character
 97  # single @ sign on behalf of the target file (colon: to the right) $ @ on behalf of the destination file name expands to a character
 98  # @ sign after take command, said: this statement is executed, but not realistic 
 99  # For example: @ $ (CC) $ (OBJS) $ (CFLAGS) $ (the Bin)   
 100  # perform only link command, but this is not print the string to the terminal
 101  # keywords: @echo represented by the command line display is not only performed. 
102  # Note: Although only, but he still will execute the command to parse text requirements,
 103  # just do not execute it, if you need to use the output string '' a reference to the contents
 104  # symbol $ Key: Indicates Escape in the makefile wherever will be identified as an escape character,
 105  # $ symbol indicates if you want, you need to use $$ 
 106 # example: @echo '$$$$' terminal will enter: " $$" 
107# Rest makefile knowledge refer to "write MakeFile with me" ---- Chen Hao  
 108  # ############################## ###########################################
 109                                   
View Code

 

Guess you like

Origin www.cnblogs.com/kimalittlestar/p/11540683.html