新手學堂:Linux下Makefile檔案簡單概念

來源:互聯網
上載者:User

 

將各個模組的關係寫進makefile,並且寫明了編譯命令,這樣,當有模組的原始碼進行修改後,就可以通過使用make命令運行makefile檔案就可以進行涉及模組修改的所有模組的重新編譯,其他模組就不用管了。

makefile檔案的寫法:

目標, 組件

規則

例如 有下面5個檔案:

/* main.c */

#include "mytool1.h"

#include "mytool2.h"

int main(int argc,char **argv)

{

mytool1_print("hello");

mytool2_print("hello");

}

/* mytool1.h */

#ifndef _MYTOOL_1_H

#define _MYTOOL_1_H

void mytool1_print(char *print_str);

#endif

/* mytool1.c */

#include "mytool1.h"

void mytool1_print(char *print_str)

{

printf("This is mytool1 print %s",print_str);

}

/* mytool2.h */

#ifndef _MYTOOL_2_H

#define _MYTOOL_2_H

void mytool2_print(char *print_str);

#endif

/* mytool2.c */

#include "mytool2.h"

void mytool2_print(char *print_str)

{

printf("This is mytool2 print %s",print_str);

}

可以這樣進行編譯以便運行main這個可執行檔

gcc -c main.c (產生main.o)

gcc -c mytool1.c (產生mytool1.0)

gcc -c mytool2.c (產生mytool2.0)

gcc -o main main.o mytool1.o mytool2.o (產生main)

也可以這樣寫makefile檔案

main main.o mytool.o mytool2.o

gcc -0 $@ $^

main.0 main.c mytool1.h mytool2.h

gcc -c $<

mytool1.0 mytool1.c mytool1.h

gcc -c $<(或者是mytool.c)

mytool2.0 mytool2.c mytool2.h

gcc -c $<(或者是mytool2.c)

通過make命令可以運行該檔案,也就是進行編譯了。

linux上有很多庫,c語言編寫的各種庫的總稱為libc,glibc為libc的一個子集,由gnu提供,核心提供的系統函數和系統調用是不包括在libc中。

linux系統預設會安裝glibc

glibc中

常用庫gcc會自動去尋找,不予理會。

在/lib, /usr/lib, /usr/local/lib 在這三個路徑下面有一些標準庫,只需-l+庫名 可以不必要指定路徑。其他庫必須在用gcc時用-L+具體的路徑。

聯繫我們

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