• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            我的CPP之路

            路漫漫其修遠兮
            隨筆 - 42, 文章 - 0, 評論 - 16, 引用 - 0
            數據加載中……

            [轉]一個簡單的makefile示例及其注釋

            http://www.cnblogs.com/sld666666/archive/2010/04/08/1707789.html
            一個簡單的makefile示例及其注釋
            posted @ 2010-04-08 21:29 sld666666 閱讀(698) 評論(2)

              相信在unix下編程的沒有不知道makefile的,剛開始學習unix平臺下的東西,了解了下makefile的制作,覺得有點東西可以記錄下。
              下面是一個極其簡單的例子:
              現在我要編譯一個Hello world,需要如下三個文件:
              1. print.h
                  #include<stdio.h>
                  void printhello();

              2. print.c
                  #include"print.h"
                  void printhello(){
                    printf("Hello, world\n");
                  }

              3. main.c
                  #include "print.h"
                  int main(void){
                    printhello();
                    return 0;
                  }

              好了,很簡單的程序了。如果我們想要編譯成功需要哪些步驟呢?
              我認為在這里需要理解的就兩步:
              #  為每一個 *.c文件生成 *o文件。
              #  連接每一個*o文件,生成可執行文件。
              下面的makefile 就是根據這樣的原則來寫的。


            一:makefile 雛形:

             
            #makefile的撰寫是基于規則的,當然這個規則也是很簡單的,就是:
            #target : prerequisites
              command  //任意的shell 命令

            實例如下:
            makefile:
                helloworld : main.o print.o #helloword 就是我們要生成的目標
                             # main.o print.o是生成此目標的先決條件
                  gcc -o helloworld main.o print.o#shell命令,最前面的一定是一個tab鍵

                mian.o : mian.c print.h
                  gcc -c main.c
                print.o : print.c print.h
                  gcc -c print.c
                
                clean :          
                    rm helloworld main.o print.o
              OK,一個簡單的makefile制作完畢,現成我們輸入 make,自動調用Gcc編譯了,  輸入 make clean就會刪除 hellowworld mian.o print.o


            二:小步改進:


              在上面的例子中我們可以發現 main.o print.o 被定義了多處,我們是不是可以向C語言中定義一個宏一樣定義它呢?當然可以:
            makefile:
                objects =  main.o print.o #應該叫變量的聲明更合適

                helloworld : $(objects) //聲明了變量以后使用就要$()了
                  gcc -o helloworld$(objects)
                 mian.o : mian.c print.h
                  gcc -c main.c
                print.o : print.c print.h
                  gcc -c print.c
                
                clean :          
                    rm helloworld $(objects)
            修改完畢,這樣使用了變量的話在很多文件的工程中就能體現出方便性了。


            三:再進一步:


              再看一下,為沒一個*.o文件都寫一句gcc -c main.c是不是顯得多余了,能不能把它干掉?而且 main.c 和print.c都需要print.h,為每一個都寫上是不是多余了,能不能再改進?
              能,當然能了:
            makefile:
                 objects =  main.o print.o

                helloworld : $(objects)
                  gcc -o helloworld$(objects)
                
                $(objects) : print.h # 都依賴print.h
                 mian.o : mian.c  #干掉了gcc -c main.c 讓Gun make自動推導了。
                print.o : print.c     
                clean :          
                    rm helloworld $(objects)

            好了,一個簡單的makefile就這樣完畢了,簡單吧。

            =========華麗的分割線=========
            還有一個網絡人氣很高的makefile教程(更詳細):
            http://blog.csdn.net/haoel/archive/2004/02.aspx
            跟我一起寫 Makefile - 陳皓

            還有法語版本的makefile介紹:
            http://gl.developpez.com/tutoriel/outil/makefile/
            Introduction à Makefile

            posted on 2010-12-24 08:48 yanvenhom 閱讀(1038) 評論(0)  編輯 收藏 引用 所屬分類: Makefile

            久久精品国产清高在天天线| 亚洲精品国精品久久99热| 少妇人妻88久久中文字幕| 色欲久久久天天天综合网| 国产精品久久国产精品99盘| 亚洲国产成人久久综合一| 色悠久久久久久久综合网 | 狠狠色丁香久久综合五月| 久久综合九色综合久99| 色播久久人人爽人人爽人人片aV| 久久精品国产精品亚洲精品| 久久91精品国产91久久麻豆| 亚洲国产成人精品女人久久久 | 精品一区二区久久久久久久网站| 久久性生大片免费观看性| 久久精品国产亚洲AV高清热 | 午夜视频久久久久一区| 俺来也俺去啦久久综合网| 99久久这里只精品国产免费| 色综合久久88色综合天天| 亚洲精品无码专区久久久| 欧美精品一区二区久久| 久久夜色tv网站| 99精品国产在热久久无毒不卡 | 久久这里都是精品| 久久99精品久久久久久齐齐| 国产精品一久久香蕉国产线看| 2020久久精品亚洲热综合一本| 精品国产91久久久久久久a| 国产一久久香蕉国产线看观看| 成人综合久久精品色婷婷| 四虎久久影院| 一级a性色生活片久久无少妇一级婬片免费放| 浪潮AV色综合久久天堂| 婷婷久久久亚洲欧洲日产国码AV| 亚洲精品国产综合久久一线| 日本亚洲色大成网站WWW久久| 久久精品国产亚洲7777| 欧美久久一区二区三区| 亚洲国产精品综合久久网络| 久久精品亚洲乱码伦伦中文|