Common makefile Writing Method

Source: Internet
Author: User

Original article address: blog.csdn.net/ghostyu/article/details/7755177

1. Target name. Do not set the target name manually.

[CPP]View plaincopy
  1. Target = $ (notdir $ (curdir ))
  2. ALL: $ (target)
  3. 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
  1. Base_dir =/root/dm36x
  2. Include $ (base_dir)/rules. Make
  3. Xdc_path = $ (dmai_install_dir)/Packages
[CPP]View plaincopy
  1. 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
  1. Sources = $ (wildcard *. c)
  2. 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
  1. 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
  1. Verbose = @
  2. Compile. c = $ (verbose) $ (mvtool_prefix) GCC $ (c_flags) $ (cpp_flags)-C
  3. Link. c = $ (verbose) $ (mvtool_prefix) GCC $ (ld_flags)

 

6. Compile

[CPP]View plaincopy
  1. $ (Objfiles): %. O: %. C $ (headers)
  2. @ Echo compiling [email protected] from $ <..
  3. $ (Compile. c)-O [email protected] $ <

7,

[CPP]View plaincopy
  1. Install: $ (if $ (wildcard $ (target), install _ $ (target ))
  2. Install _ $ (target ):
  3. @ Install-d $ (exec_dir)
  4. @ Install $ (target) $ (exec_dir)
  5. @ Install export (targetcmd.txt $ (exec_dir)
  6. @ Echo
  7. @ 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

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.