linux下的C語言開發(makefile編寫) 02

來源:互聯網
上載者:User

【 聲明:著作權,歡迎轉載,請勿用於商業用途。  聯絡信箱:feixiaoxing @163.com】

    對於程式設計員來說,makefile是我們繞不過去的一個坎。可能對於習慣Visual C++的使用者來說,是否會編寫makefile無所謂。畢竟工具本身已經幫我們做好了全部的編譯流程。但是在Linux上面,一切變得不一樣了,沒有人會 為你做這一切。編代碼要靠你,測試要靠你,最後自動化編譯設計也要靠你自己。想想看,如果你下載了一個開源軟體,卻因為自動化編譯失敗,那將會在很大程度 上打擊你學習代碼的自信心了。所以,我的理解是這樣的。我們要學會編寫makefile,至少會編寫最簡單的makefile。

    首先編寫add.c檔案,

[cpp] view plaincopy
  1. #include "test.h"  
  2. #include <stdio.h>  
  3.   
  4. int add(int a, int b)  
  5. {  
  6.     return a + b;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     printf(" 2 + 3 = %d\n", add(2, 3));  
  12.     printf(" 2 - 3 = %d\n", sub(2, 3));  
  13.     return 1;  
  14. }  

    再編寫sub.c檔案,

[cpp] view plaincopy
  1. #include "test.h"  
  2.   
  3. int sub(int a, int b)  
  4. {  
  5.     return a - b;  
  6. }  

    最後編寫test.h檔案,

[cpp] view plaincopy
  1. #ifndef _TEST_H  
  2. #define _TEST_H  
  3.   
  4. int add(int a, int b);  
  5. int sub(int a, int b);  
  6. #endif  

    那麼,就是這三個簡單的檔案,應該怎麼編寫makefile呢?

[cpp] view plaincopy
  1. test: add.o sub.o  
  2.     gcc -o test add.o sub.o  
  3.   
  4. add.o: add.c test.h  
  5.     gcc -c add.c  
  6.   
  7. sub.o: sub.c test.h  
  8.     gcc -c sub.c      
  9.       
  10. clean:  
  11.     rm -rf test  
  12.     rm -rf *.o 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.