[Program construction of Linux development technology] makefile Learning (continuously updated)

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1. Entry

1) makefile: Helps compile multiple files.

2) basic compiling example: Take the following four files as an example.

//main.c#include"mytool1.h"#include"mytool2.h"int main(int argc,char **argv){mytool1_print("hello");mytool2_print("hello");}//mytool1.h#ifndef _MYTOOL_1_H#define _MYTOOL_1_Hvoid mytool1_print(char *print_str);#endif//mytool1.c#include"mytool1.h"void mytool1_print(char *print_str){printf("This is mytool1 print %s",print_str);}//mytool2.h#ifndef _MYTOOL_2_H#define _MYTOOL_2_Hvoid mytool2_print(char *print_str);#endif//mytool2.c#include"mytool2.h"void mytool2_print(char *print_str){printf("This is mytool2 print %s",print_str);} 

The makefile for compilation and construction is

main:main.o mytool1.o mytool2.ogcc -o main main.o mytool1.o mytool2.omain.o:main.c mytool1.h mytool2.hgcc -c main.cmytool1.o:mytool1.c mytool1.hgcc -c mytool1.cmytool2.o:mytool2.c mytool2.hgcc -c mytool2.c



The essence of makefile is to describe the dependencies and compilation methods between the source code involved in the construction. The general format is as follows:

Target: Components

Tab rule

The first line indicates the dependency. The second line is the rule.

Main: Main. O mytool1.o mytool2.o is the so-called dependency relationship. In fact, it is a method to solve the problem. We recommend that you draw a dependency graph on the paper, for example:


--- Main. O -- (main. C, mytool1.h, mytool2.h)


Mytool2.o -- (mytool2.c, mytool2.h)


Mytool1.o -- (mytool1.c, mytool1.h)

Gcc-O main. O mytool1.o mytool2.o is the so-called rule.

3) simplified

$ @ -- Target file, $ ^ -- all dependent files, $ <-- the first dependency file. The substitution rules can be abbreviated as follows:

main:main.o mytool1.o mytool2.ogcc -o $@ $^main.o:main.c mytool1.h mytool2.hgcc -c $<mytool1.o:mytool1.cmytool1.hgcc -c $<mytool2.o:mytool2.cmytool2.hgcc -c $<

If there are regular naming rules, you can abbreviated them as follows:

main:main.o mytool1.o mytool2.ogcc -o $@ $^..c.o:gcc -c $<

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.