Makefile 檔案格式,makefile檔案格式

來源:互聯網
上載者:User

Makefile 檔案格式,makefile檔案格式

Makefile包括 目標檔案、依賴檔案、可執行命令三部分。

每部分的基本格式如下:

test: prog.o  code.o

        gcc  -o  test   prog.o   code.o

        

其中,第一行的test是目標檔案; prog.o、code.o是依賴檔案;

           第二行的gcc -o test prog.o code.o是可執行命令。

整個Makefile檔案都是這種格式。


以下是一些example:

-----------------------Makefile example 1----------------------------------

#this line is the comment for the Makefile

test: prog.o code.o

        gcc   prog.o  code.o  -o test

prog.o: prog.c  prog.h  code.h

         gcc  -c  prog.c  -o  prog.o

code.o: code.c  code.h

        gcc  -c  code.c  -o  code.o

clean:

        rm  -f  *.o

----------------------------------------------------------------------------------------------


-------------------------------example 2(包含變數)---------------------------------

#this line is the Makefile comment

OBJS = prog.o  code.o

CC = gcc

CFLAGS = -Wall -g -O

test: ${OBJS}

        ${CC}  ${CFLAGS}  ${OBJS}  -o test

prog.o: prog.c  prog.h  code.h

        ${CC}  ${CFLAGS}  -c prog.c  -o prog.o

code.o: code.c  code.h

        ${CC}   ${CFLAGS}  -c code.c  -o code.o

clean:

        rm  -f  *.o 

------------------------------------------------------------------------------------------------




-------------------------------example 3(使用Makefile的隱含規則)---------------------------------

1,如果沒有相應的編譯命令,則使用隱含規則,所有的 ".c檔案" 編譯成與它名稱相同的 ".o檔案"。

2, 使用Makefile的自動變數。


#this line is the Makefile comment

OBJS = prog.o  code.o

CC = gcc

test: ${OBJS}

        ${CC}   -o  $@   $^

prog.o: prog.c  prog.h  code.h             #no exec command,and will generate the prog.o

code.o: code.c  code.h                        #no exec command,and will generate the code.o

clean:

        rm  -f  *.o 

------------------------------------------------------------------------------------------------



-----------------------------------------------------以下為我測試過的執行個體檔案內容 :-----------------------------------------------------

========================== Makefile ===============

#this line is the comment
CC = gcc
OBJS = my_str.o
CFLAGS = -Wall -g -O


program: my_main.c ${OBJS}
               ${CC} ${CFLAGS} $^ -o $@ 
my_str.o: my_str.c my_str.h
               ${CC} ${CFLAGS} -c my_str.c -o my_str.o


clean:
               rm -f *.o

=======================================================================


==========================my_main.c=======================

#include <unistd.h>
#include <stdlib.h>
#include "my_str.h"

int 
main(int argc, const char **argv)
{
if(my_cmp(argv[1], argv[2]) == 0)
write(1, "Equal !\n", sizeof("Equal !\n"));
else
write(1, "Not Equal !\n", sizeof("Not Equal !\n"));


exit(0);
}

===============================================================


========================my_str.c=============================

#include "my_str.h"


int 
my_cmp(const char *str1, const char *str2)
{
if(!str1 || !str2)
return -1;
while(*str1 && *str2 && *str1 == *str2)
str1++, str2++;
return *str1 - *str2;
}

==================================================================


===================my_str.h============================================

#ifndef _MY_STR_H
#define _MY_STR_H


int my_cmp(const char *str1, const char *str2);


#endif

==================================================================


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.