學習 Linux的makefile

makefile 是個工程管理器。

make命令執行時,需要一個 Makefile 文件,以告訴make命令需要怎麼樣的去編譯和鏈接程序。

makefile 格式 :

target: dependency_files

command (在此行開始要按TAB,再寫command)

第一步:建立3個文件,包括fun.c ,fun.h,main.c

/*header file */
#include<stdio.h>
extern int max_fun(int x,int y);
extern void copy(char *src,char *dest);


/*fun.c  return max number*/
//#include<fun.h>
#include"fun.h"
int  max_fun(int x,int y)
{
        return x>y?x:y;
}


/*main.c fuction*/
#include<stdio.h>
#include"fun.h"
int main(int argc,char **argv)
{
        int a,b;
        char src[50],dest[50];
        printf("Enetr the number  a and b \n");
        scanf("%d%d",&a,&b);
        int max=max_fun(a,b);
        printf("the max number is %d\n",max);


        printf("Enetr input the string :");
        scanf("%s",src);
//      fflush(stdin);
//      fgets(src,50,stdin);
        copy(src,dest);
        printf("copy the string is :%s\n",dest);
        return 0;
}

第二步:vi makefile 將以下方法之一複製到makefile下,執行make 會得到 我們想要的目標文件main(可執行文件),運行./main 

方法1:

#makefile write 

#printf object main
main:main.o fun.o
        gcc main.o fun.o -o main
main.o:main.c fun.h
        gcc -c  main.c -o main.o
fun.o:fun.c fun.h
        gcc -c fun.c -o fun.o
.PHONY :clean
clean:

        rm -f main *.o

方法二:

#makefile write 
#put main
OBJS=main.o fun.o
CC=gcc
CFLAGS=-c
main:$(OBJS)
        #$(CC) $(CFLAGS) -o main
        $(CC) $(OBJS) -o main
main.o:main.c fun.h
        $(CC) $(CFLAGS) main.c -o main.o 
fun.o:fun.c fun.h
        $(CC) $(CFLAGS) fun.c -o fun.o 
###.PHONY :clean
clean:
        rm -f main *.o
方法三:      

#makefile auto variable 
#put main
OBJS=main.o fun.o
CC=gcc
CFLAGS=-c
main:$(OBJS)
        $(CC) $^ -o $@
main.o:main.c fun.h
        $(CC) $(CFLAGS) $< -o $@
fun.o:fun.c fun.h
        $(CC) $(CFLAGS) $< -o $@ 
###.PHONY :clean
clean:
        rm -f main *.o

方法四:

#makefile NO show guizhe
#put main
OBJS=main.o fun.o
CC=gcc
CFLAGS=-c
main:$(OBJS)
        $(CC) $^ -o $@
#main.o:main.c fun.h
#       $(CC) $(CFLAGS) $< -o $@
#fun.o:fun.c fun.h
#       $(CC) $(CFLAGS) $< -o $@ 
###.PHONY :clean
clean:
        rm -f main *.o
方法五:                                                                           
#makefile NO show guizhe
#put main
OBJS=main.o fun.o
CC=gcc
CFLAGS=-c
main:$(OBJS)
        $(CC) $^ -o $@
###.PHONY :clean
clean:
        rm -f $(OBJS) *.o                                                                           
~ 方法 六:      

#makefile  mode guizhe
#put main
OBJS=main.o fun.o
CC=gcc
CFLAGS=-c
main:$(OBJS)
        $(CC) $^ -o $@
%.o:%.c
        $(CC) $(CFLAGS) $< -o $@
###.PHONY :clean
clean:
        rm main  $(OBJS)

、、、、、、、、、、、、、好用的makefile 、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

方法最好用的:(再增加一個copy.c,在fun.h頭文件增加 extern void copy(char *,char *))

#include"fun.h"
void copy(char *src,char *dest)
{
/*      int i=0;
 *      while(*(src+i)!='\0')
                //(*dest)++=(*src)++;
                *dest++=*src++;//前置++,執行完在加
        *dest='\0';     
*/
        printf("start copy string :\n");
        while(*dest++=*src++)
        ;
        printf("copy success\n");
}


#makefile  no show  guizhe(隱式規則)
#put objects file : main

makefile 如下:
#OBJS=main.o fun.o
OBJS=main.o fun.o copy.o   //增加copy.o 就可以了,和.c文件名要一樣
CC=gcc
CFLAGS=-c
main:$(OBJS)
        $(CC) $^ -o $@  //$^ is all dependency files,$@is object file
#.PHONY :clean
clean:
        rm main  $(OBJS)   // clear  main  and all .o file 

發佈了14 篇原創文章 · 獲贊 4 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章