Makefile (I) for Linux development tools and makefile for development tools

Source: Internet
Author: User

Makefile (I) for Linux development tools and makefile for development tools
Ii. makefile (I) 01. make tool can use make tool to automatically complete compilation. These tasks include: If several source files are modified, only the new source files are reinstalled. If a header file is modified, all the source files containing the header file are recompiled. Using this automatic compilation can greatly simplify the development work and avoid unnecessary re-compilation. The make tool uses a file called Makefile to complete and automatically maintain the compilation. The Makefile file describes the compilation and linking rules of the entire project. 02. basic Makefile rules: TARGET...: DEPENDENCIES... COMMAND... the object generated by the TARGET program, how to execute the file and the TARGET file, the object can also be an action to be executed, such as clean, and also become a pseudo TARGET. DEPENDENCIES is a list of input files used to generate the target. A target usually depends on multiple files. Command is the action that make executes (the command is a shell command or a program that can be executed in shell ). Note: The start character of each command line must be a TAB character! If one or more DEPENDENCIES files are updated, the COMMAND is executed. This is the core content of Makefile # touch main. c add. c sub. c add. h sub. h # lsmain. c add. c sub. c add. h sub. h # vi main. cint main () {return 0 ;}# vi Makefilemain: main. o add. o sub. ogcc-Wall-g main. o add. o sub. o-o mainmain. o: main. cgcc-Wall-g-c main. c-o main. oadd. o: add. c add. hgcc-Wall-g-c add. c-o add. osub. o: sub. c sub. hgcc-Wall-g-c sub. c-o sub. o # make (the first target is generated by default) # lsmain # make (the file has not changed) make: 'main' id up to date # touch sub. h (regenerate sub. h) # makegcc-Wall-g-c sub. c-o sub. ogcc-Wall-g main. o add. o sub. o-o mainclean: (the pseudo target is not the file to be generated, used to delete these files) rm-f main. o add. o sub. o # make clean delete all linked files # make main. o only generate main. o this file. PHONY: clean (the beginning of the Makefile file. indicates that clean is a pseudo-target.) If a clean file exists in the folder where Makefile is located, the system prompts: make: 'clean' is up to date1_3.makefile automation variable $ @: target File Name of the Rule $ <: name of the first dependent file of the Rule $ ^: list of all dependent files of the rule # vi Makefile.1.PHONY: cleanOBJECTS = main. o add. o sub. omain: $ (OBJECTS) gcc-Wall-g $ ^-o $ @ main. o: main. cgcc-Wall-g-c $ <-o $ @ add. o: add. c add. hgcc-Wall-g-c $ <-o $ @ sub. o: sub. c sub. hgcc-Wall-g-c $ <-o $ @ clean: @ (add @ will not print on the screen) echo "begin delete... "(the command still needs to be executed) rm-f main $ (OBJECTS) # make (working normally) # make clean (working normally) # make clean-f Makefile.1 (execute the clean command according to the rules of Makefile.1) begin delete... rm-f main. o add. o sub. o04.Makefile: Multiple Executable File mode rules: %. o: %. c suffix rules :. c. o # mkdir 01 # mv *. c 01 # ls # mv *. * 01 # ls01 Makefile # mv Makefile 01 # ls # mkdir 02 # cd 02 # touch 01test. c 02test. c (01test. c, 02test. c) # vi 01test. c int main (void) {return 0 ;}# vi02test. cint main (void) {return 0 ;}# vi Makefile. PHONY: cleanBIN = 01 test 02 testall: $ (bin) 01 test: 01test. ogcc-Wall-g $ ^-o $ @ 02 test: 02test. ogcc-Wall-g $ ^-o $ @ clean: rm-f *. o $ (BIN) # make01test 02 test # vi MakefileBIN depends on the two files all. The target depends on the two files BIN. the compiler will automatically "*. c "file generation executable file with the same name # makecc 01test. c-o 01 testcc 02test. c-o 02 test # make clean # ls01test. c 02test. c Makefile # makecc-c-o 01test. o 01test. cgcc-Wall-g-c 01test. o-o 01 test # make clean # ls01test. c 02test. c Makefile # vi Makefile %. o: %. c. c file generation. o file) gcc-Wall-g-c $ <-o $ @ # make clean # make # ls # vi MakefileCC = gccCFLAGS =-Wall-g # %. o: %. c # $ (CC) $ (CFLAGS)-c $ <-o $ @. c. o: $ (CC) $ (CFLAGS)-c $ <-o $ @ 01 test: 01test. o $ (CC) $ (CFLAGS) $ ^-o $ @ 02 test: 02test. o (locate the cursor here, enter 2yy, and copy two rows) $ (CC) $ (CFLAGS) $ ^-o $ @ (Click p and copy it here) # make clean # make # ls # vi 03test. cint mian (void) {return 0 ;}

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.