linux 下makefile的使用舉例

來源:互聯網
上載者:User

該程式在linux平台下用C語言實現

有兩個標頭檔m1.h m2.h 對應的實現檔案m1.c m2.c  調試檔案test.c

案例代碼如下:

m1.h

#include<stdio.h>void m1_print(char *str);

m2.h

#include<stdio.h>void m2_print(char *str);

m1.c

#include<stdio.h>#include"m1.h"void m1_print(char *str){printf("this is string %s\n",str);}

m2.c

#include<stdio.h>#include"m2.h"void m2_print(char *str){printf("this is string %s.",str);}

test.c

#include<stdio.h>#include"m1.h"#include"m2.h"void main(){m1_print("Hello");m2_print("World");}

Makefile檔案兩種寫法:

1.

main:test.o m1.o m2.ogcc -o testt test.o m1.o m2.otest.o:test.c m1.h m2.hgcc -c test.cm1.o:m1.c m1.hgcc -c m1.cm2.o:m2.c m2.hgcc -c m2.c

2. 

ma:test.o m1.o m2.ogcc -o $@ $^test.o:test.c m1.h m2.hgcc -c $<m1.o:m1.c m1.hgcc -c $<m2.o:m2.c m2.hgcc -c $<

注意:    1. 三個常用變數:$@(目標檔案),$^(所有依賴檔案),$<(第一個依賴檔案)

  2.命令列處需要Tab鍵空行

3.makefile檔案中編譯命令其實就是一個遞迴過程

4. 編譯指令make 或 make -f  Makefile

相關文章

聯繫我們

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