[Linux] makefile for C Programming

Source: Internet
Author: User

I. Example

Four files: Main. C; Main. h; t_print.c; t_print.h

Makefile: Statement 1)

main:main.o t_print.ogcc main.o t_print.o -o main main.o:main.c main.hgcc -c main.ct_print.o:t_print.c t_print.hgcc -c t_print.cclean:rm  main.o t_print.o

Run make or make main.

Delete intermediate files: Make clean

Statement 2)

main:main.o t_print.ogcc main.o t_print.o -o main main.o:main.c main.hgcc -c main.ct_print.o:t_print.c t_print.hgcc -c t_print.c

Run make or make main.

Statement 3)

main:main.o t_print.ogcc main.o t_print.o -o main 

Run make or make main.

The make command automatically derives the files required for Main. O generation.

Statement 4)

object=main.o t_print.omain:$(object)gcc $(object) -o main clean:rm  $(object)

Variables are used here, but the dependency is unclear.

[Note] the tab Space key is in front of GCC.

Ii. Make Execution Process

1) make finds the "makefile" or "makefile" file in the current directory.

2) if it is found, it will find the first target file in the file (target ). In the above example, main

3) if the execution of the main command depends on the file generated by the subsequent command execution, the following command is executed first.

4) when the file required by the main command is generated, run the main command.

Iii. Details

1) You can use other file names as makefiles, such as make. Linux.

Make-F make. Linux //-F file specifies the file

2) makefile can also contain other files, such

Makefile:

object=main.o t_print.omain:$(object)gcc $(object) -o main clean:rm  $(object)include aa

AA:

cleanmain:rm main

Run: Make cleanmain to execute rm main

Note: When the make command starts, other files pointed out by include will be searched, and the content in the file will be placed in the current location.

3) Add "-" before a command in makefile to indicate that no error is reported and the execution continues regardless of the error.

-Rm main. O t_print.o // you can write it as a wildcard RM *. O.

----- The night is already deep ---- not yet resumed -----










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.