makefile usage

concept

Order to define rules, specify the file to compile and link, and tell make, also known as automated compilation.

It contains five content

1. explicit rules,

2. obscure rules,

3. Variable definitions

4. The file indicates

5. Comment

If you already have compiled o file before make. You can use make clean

Before or with three files

tool.h tool.c main.c
在tool.h中

int find_max(int arr[],int n);

In the tool.c

#include "tool.h"

int find_max(int arr[],int n){
        int max= arr[0];
        int i;
        for(i= 0;i<n;i++){
                if(arr[i]>max){
                        max= arr[i];
                }
        }
        return max;
}

In main.c,

#include <stdio.h>
#include "tool.h"

int main(){
        int arr[]= {1,3,5,8,2};
        int max= find_max(arr,5);
        printf("max=%d\n",max);
}

Makefile without first compiling a wave

Command is as follows:

gcc -c tool.c
gcc -c main.c
gcc -o main main.o tool.o
./main

Then edit the makefile:

Previous define func indicate how then declared the makefile and use of reference and no-argument constructor

define func
$(info "hello")
endef
$(call func )


define func1
$(info $(1) $(2))
endef

$(call func1,hello,world)

Defined objects in order to later if the variable is changed .o file as long as the time and then change variables defined on OK

In fact the make command is

objects = main.o tool.o

main: $(objects)
        gcc $(objects) -o main

Make clean when executed perform the following:

objects = main.o tool.o

.PHONY:clean
clean:
        -rm main $(objects)

.PHONY: indicates that a clean display for the goal, and to avoid duplicate names compile time error file

Because this target will not generate a clean file for the target's name and file name can not duplicate it needs .PHONY

Throughout the makefile as follows

define func
$(info "hello")
endef
$(call func )


define func1
$(info $(1) $(2))
endef

$(call func1,hello,world)
objects = main.o tool.o

main: $(objects)
        gcc $(objects) -o main
        
.PHONY:clean
clean:
        -rm main $(objects)
Published 137 original articles · won praise 29 · views 110 000 +

Guess you like

Origin blog.csdn.net/xiexiaotian11/article/details/103872181