[Linux] -- compile a simple makefile in Linux

Source: Internet
Author: User

Today, I learned how to write makefile in C language programming in Linux. This is just a simple example:

Assume that the following file is in the practice Directory: Main. c hello1.c hello2.c hello1.h hello2.h several program files, to compile and connect them, the best way is to write a MAKEFILE file and run it with the make command. Make is a command tool that explains commands in makefile. Once the MAKEFILE file is written, you only need a make command to automatically compile the entire project, greatly improving the efficiency of software development. The MAKEFILE file in this example is as follows:

Main: Main. O hello1.o hello2.o

Gcc-O main. O hello1.o hello2.o (there is a tab key in front)

Main.0: Main. c hello1.h hello2.h

Gcc-C main. c

Hello1.o: hello1.c hello1.h

Gcc-C hello1.c

Hello2.o: hello2.c hello2.h

Gcc-C hello2.c

Clean:

Rm main. O hello1.o hello2.o

At this time, you only need a make command to generate the final result: the executable file main

Note:

1. to regenerate the target code, run the "make clean" command.

2. A more robust clean statement is as follows:

. Phony: clean

Clean:

-Rm main. O hello1.o hello2.o

3. Several target files can also be written as variables, namely:

At the beginning of the MAKEFILE file, write: Objects = main. O hello1.o hello2.c. You can use "$ (objects) to replace it elsewhere!

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.