歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 闡述Linux Makefile文件概念

闡述Linux Makefile文件概念

日期:2017/2/28 16:39:13   编辑:Linux教程

Linux系統越來越受到電腦用戶的歡迎,於是很多人開始學習Linux時,學習Linux,你可能會遇到Linux makefile問題,這裡將介紹Linux makefile問題的解決方法,在這裡拿出來和大家分享一下。

將各個模塊的關系寫進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\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);
}

可以這樣進行編譯以便運行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+具體的路徑。通過本文你就能全面了解Linux makefile。

Copyright © Linux教程網 All Rights Reserved