Get started with makefile

Source: Internet
Author: User

1. First, take a look at the simplest C file // hello. c file # include void main () {printf ("hello world \ n");} is hello. c. Compile the makefile file, which is compiled using gcc.

  • $ Vi makefile
  • Hello: hello. c
  • Gcc-o hello. c
We can see that the simplest makefile only needs two lines. We can see row 1st: hello is the file to be generated, hello. c is the source file required for compilation, separated by: 2nd lines in the middle: is the specific compilation command 2. We can also use gcc to put hello first. c is compiled into a machine language. o, and then link each file to generate the makefile file above the binary file, which can be written as follows:
  • Hello: hello. o
  • Gcc-o hello. o
  • Hello. o: hello. c
  • Gcc-c hello. c
From the code above, we can see that the last compiled hello is by hello. o The Last generated line 3, 4: 3rd, hello. c compile and generate hello. o, Row 3: Generate hello. o For specific commands, see line 1 and line 2: Line 2. the binary file hello requires hello. o, line 3, is it easy to generate the specific hello command. 3. If you need to link multiple files, you only need to generate them. o file, and finally generate the final file such as: file1.h, file1.c, file2.h, file2.c, main. c five files

The makefile file is as follows:

  • Main: main. o file1.o file2.o
  • Gcc-o main. o file1.o file2.o
  • Main. o: main. c file1.h file2.h
  • Gcc-c main. c
  • File1.o: file1.h file1.c
  • Gcc-c file1.c
  • File2.o: file2.h file2.c
  • Gcc-c file2.c
From the code above, we can see that we need to compile the file1.o, file2.o, and main. o files, and finally generate the final main file through the link.

 

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.