Original article address: blog.csdn.net/ghostyu/article/details/7755177
1. Target name. Do not set the target name manually.
[CPP]View plaincopy
- Target = $ (notdir $ (curdir ))
- ALL: $ (target)
- CMD ....
$ (Curdir) indicates the full path of the current directory of the makfile
$ (Notdir $ (PATH) indicates that the path directory is removed and only the current directory name is left
In this way, you can get the current directory name of makefile. Using the directory name as the target program name is a good choice.
2. Use include to include all makefile sharing settings
[CPP]View plaincopy
- Base_dir =/root/dm36x
- Include $ (base_dir)/rules. Make
- Xdc_path = $ (dmai_install_dir)/Packages
[CPP]View plaincopy
- Dmai_install_dir has been set in rules. Make. In this case, you only need to include this rules. Make when using the dmai_install_dir variable.
3. traverse all specific source files
[CPP]View plaincopy
- Sources = $ (wildcard *. c)
- Headers = $ (wildcard *. h)
If the current directory contains main. c func. c func. h
In this way, the sources variable is equal to main. c func. C.
The headers variable is equal to func. h.
In this way, you do not need to modify the makefile every time you add the source file.
4. Replace the file name
[CPP]View plaincopy
- Objfiles = $ (sources: %. c = %. O)
If sources, etc., and Main. C and func. c In 3
In this way, objfiles is equal to main. O func. O.
Also get rid of manually modifying the compiled intermediate file name
5. Cross-compilation settings
[CPP]View plaincopy
- Verbose = @
- Compile. c = $ (verbose) $ (mvtool_prefix) GCC $ (c_flags) $ (cpp_flags)-C
- Link. c = $ (verbose) $ (mvtool_prefix) GCC $ (ld_flags)
6. Compile
[CPP]View plaincopy
- $ (Objfiles): %. O: %. C $ (headers)
- @ Echo compiling [email protected] from $ <..
- $ (Compile. c)-O [email protected] $ <
7,
[CPP]View plaincopy
- Install: $ (if $ (wildcard $ (target), install _ $ (target ))
- Install _ $ (target ):
- @ Install-d $ (exec_dir)
- @ Install $ (target) $ (exec_dir)
- @ Install export (targetcmd.txt $ (exec_dir)
- @ Echo
- @ Echo installed $ (target) binaries to $ (exec_dir )..
First, check whether the current directory has $ (target). If yes, run the pseudo-target install _ $ (target). If this is expanded, it is equivalent to install_app. If $ (target) is equal to app
Common makefile Writing Method