闡述Linux Makefile文件概念
Linux系統(tǒng)越來越受到電腦用戶的歡迎,于是很多人開始學(xué)習(xí)Linux時,學(xué)習(xí)Linux,你可能會遇到Linux makefile問題,這里將介紹Linux makefile問題的解決方法,在這里拿出來和大家分享一下。
將各個模塊的關(guān)系寫進(jìn)makefile,并且寫明了編譯命令,這樣,當(dāng)有模塊的源代碼進(jìn)行修改后,就可以通過使用make命令運(yùn)行makefile文件就可以進(jìn)行涉及模塊修改的所有模塊的重新編譯,其他模塊就不用管了。
makefile文件的寫法:
目標(biāo), 組件
規(guī)則
例如 有下面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\n",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\n",print_str);
}
可以這樣進(jìn)行編譯以便運(yùn)行main這個可執(zhí)行文件
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命令可以運(yùn)行該文件,也就是進(jìn)行編譯了。
Linux上有很多庫,c語言編寫的各種庫的總稱為libc,glibc為libc的一個子集,由gnu提供,內(nèi)核提供的系統(tǒng)函數(shù)和系統(tǒng)調(diào)用是不包括在libc中。
Linux系統(tǒng)默認(rèn)會安裝glibc
glibc中
常用庫gcc會自動去查找,不予理會。
在/lib, /usr/lib, /usr/local/lib 在這三個路徑下面有一些標(biāo)準(zhǔn)庫,只需-l+庫名 可以不必要指定路徑。其他庫必須在用gcc時用-L+具體的路徑。通過本文你就能全面了解Linux makefile。
【編輯推薦】