Article From:https://www.cnblogs.com/chuijingjing/p/9219399.html

Example 1

1、Analysis of source file code dependencies

mian.c

#include "test1.h"  
#include "test2.h"
#include <stdio.h>
int main()
{
        test1_func("hello test1!");
        test2_func("hello test2!");
        return 0;
}

It can be seen that the generation of main.o files requires main.c, test1.h, test2.h.

test1.h

#ifndef _TEST_1_H
#define _TEST_1_H
        void test1_func(char *str);
#endif

test1.h There is a statement in the inside of the test1.c function

test2.h

fndef _TEST_2_H
#define _TEST_2_H
        void test2_func(char *str);
#endif

test2.h There is a statement in the inside of the test2.c function

test1.c

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

void test1_func(char *str)
{
        printf("This is test1 : %s ",str);
}

Generate test1.o files, need test1.c, test1.h

test2.c

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

void test2_func(char *str)
{
        printf("This is test2 : %s",str);
}

Generate test2.o files, need test2.c, test2.h

2、Writing Makefile files directly

[root@localhost MakefileExample]# vim Makefile
#Makefile
main:main.o test1.o test2.o
        gcc -o main main.o test1.o test2.o
main.o:main.c test1.h test2.h
        gcc -c main.c
test1.o:test1.c test1.h
        gcc -c test1.c
test2.o:test2.c test2.h
        gcc -c test2.c
.PHONY:clean
clean:
        rm -f *.o main 

make Compile

Scavenging the generated.O file

3、Using variables to write Makefile files

#Makefile
OBJ=main.o test1.o test2.o
CC =gcc
test:$(OBJ)
        $(CC) -o $@ $^
main.o:main.c test1.h test2.h
        $(CC) -c $<
test1.o:test1.c test1.h
        $(CC) -c $<
test2.o:test2.c test2.h
        $(CC) -c $<
.PHONY:clean
clean:
        rm -f $(OBJ) test

makeCompile

Example two

1、Analysis of source file code dependencies

hello.c

void showhello()  {
        hello();
}

 Generating hello.o files depends on hello.c, hello.h

hello.h

/*hello.h*/
#ifndef  HELLO_H
#define  HELLO_H



void hello()   {
        star1();
        printf("hello,my friends\n");
}

#endif

hello.h The function body of the star () function

star.c

#include "starfun.h"
#include "hello.h"
#include <stdio.h>
int main() {
        star1();
        star2();
        showhello();
        return 0;
}

 To generate star.o files, you need to rely on star.c,hello.h , starfun.h file

starfun.h

/*****starfun.h*****/
#ifndef STARFUN_H
#define STARFUN_H


#define  NUM 4
#define  NUMBER 3

int star1() {
        int i,j,k;
        for(k=1;k<=NUM;++k) {
                for(i=1;i<=(NUM-k);++i)
                        printf(" ");
                for(j=1;j<=(2*k-1);++j)
                        printf("*");
                printf("\n");
        }
        return 0;
}
int star2() {
        int i,j,k;
        for(k=NUMBER;k>=0;--k)  {
                for(i=1;i<=(NUMBER-k+1);++i)
                        printf(" ");
                for(j=1;j<=(2*k-1);++j)
                        printf("*");
                printf("\n");
        }
        return 0;
}

#endif

2、Writing Makefile files directly

[root@localhost test_2]# vim Makefile
#cjj
main:hello.o star.o
        gcc -o main hello.o star.o
hello.o:hello.c hello.h
        gcc -c hello.c
star.o:star.c hello.h starfun.h
        gcc -c star.c
.PHONY:clean
clean:
        rm -f *.o main

make Compile (warning, not run)

Running the generated file

[root@localhost test_2]# ./main 

Delete.O files and main files

3、Using variables to write Makefile files

#cjj
OBJ=hello.o star.o
CC =gcc
STAR=star.c hello.h starfun.h

main:$(OBJ)
        $(CC) -o $@ $^
hello.o:hello.c hello.h
        $(CC) -c $<
star.o:$(STAR)
        $(CC) -c star.c
.PHONY:clean
clean:
        rm -f $(OBJ) main

Compile and run

 

Link of this Article: Two methods of compiling Makefile

Leave a Reply

Your email address will not be published. Required fields are marked *