make文件基礎用法

參照:https://www.jianshu.com/p/0b2a7cb9a469

創建工作目錄,包含一下文件

  • main.c
  • person.c
  • b.h
  • c.h
/***
c.h
***/
//this is c.h
/***
b.h
***/
//this is b.h
/***
main.c
***/
#include<stdio.h>
//#include"a1.h"
//#include"b.h"int main()
{printf("The Version 1.1\n");return 0;
}

如果main.c文件中加上注釋的頭文件會生成對應的b.h.gch文件,此處存疑

1.創建makefile文件:

/***
makefile
***/
app: main.o person.o other.ogcc -o app main.o person.o other.o
main.o:main.cgcc -c main.c a.h
person.o:person.c a.h b.hgcc -c person.c
other.o:person.c b.hgcc -c person.c -o other.oclean:rm app main.o person.o other.o

2.定義變量,代替目標文件,簡化代碼:

objects = main.o person.o other.o
app : $(objects)gcc -o app $(objects)
main.o : main.cgcc -c main.c c.h
person.o : person.c c.h b.hgcc -c person.c
other.o : person.c b.hgcc -c person.c  -o other.oclean :rm app $(objects)

3.使用makefile自動推導機制,簡化指令語句

?????? 在編譯過程中,哪些類型的文件的編譯需要哪些指令是固定的,所以makefile文件可以從依賴關系自動推導出后面要執行的語句

簡化如下:

objects = main.o person.oapp : $(objects)gcc -o app $(objects)main.o : main.c
person.o : person.c c.h b.hclean :rm app $(objects)

只有依賴聲明下面沒有任何語句,makefile才會自動推導,否則執行指定文件。

注意:這里去掉了other.o,是因為other.o依賴person.c手動生成的other.o,這里的makefile只會生成與依賴文件相同名字的.o文件。

4.如果文件不在一個目錄下,需要指定目錄,只需要在makefile的第一行添加

VPATH = path1 : path2 :path3

makefile文件會自動按照路徑順序,依次查找文件

如圖:

person.c文件在 ?/home/exbot/wangqinghe/makefileTest

b.h文件在/home/exbot/wangqinghe

c.h 文件在當前目錄中2019060601 文件夾下

于是makefile 如下:

VPATH = ./2019060601 :/home/exbot/wangqinghe : /home/exbot/wangqinghe/makefileTestCC = gccobjects = main.o person.oapp : $(objects)$(CC) -o app $(objects)@echo "input information : compile finished"#@echo "輸出信息:編譯完成"main.o : main.cperson.o : person.c c.h b.h.PHONY : cleanclean :     rm app $(objects)

?

轉載于:https://www.cnblogs.com/wanghao-boke/p/10986723.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385049.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385049.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385049.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

一個Linux下C線程池的實現(轉)

1.線程池基本原理 在傳統服務器結構中, 常是 有一個總的 監聽線程監聽有沒有新的用戶連接服務器, 每當有一個新的 用戶進入, 服務器就開啟一個新的線程用戶處理這 個用戶的數據包。這個線程只服務于這個用戶 , 當 用戶與服務器端關閉連接以后, 服務器端銷毀這個線程。然而頻繁地…

二維數組作為函數參數

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

mystrcat

#include<stdio.h> //如果一個數組做為函數的形參傳遞&#xff0c;那么數組可以在被調用的函數中修改 //有時候不希望這個事發生&#xff0c;所以對形參采用const參數 //size_t strlen(const char *s); //strcpy(char* s1,const char* s2); void mystrcat(char *s1,cons…

關于非阻塞的recv的時候返回的處理

注意recv&#xff08;&#xff09;如果讀到數據為0&#xff0c;那么就表示文件結束了&#xff0c;如果在讀的過程中遇到了中斷那么會返回-1&#xff0c;同時置errno為EINTR。 因此判斷recv的條件&#xff1a; 如果read返回<0 如果0 表示文件結束&…

帶參程序

windows環境 #include<stdio.h>int main(int argc, char *argv[]) {printf("argc %d\n", argc);for (int i 0; i < argc; i){printf("argv[%d] %s\n",i, argv[i]);}system("pause");return 0; } windows環境下&#xff0c;帶參函數…

Ubuntu安裝mysql步驟

1.打開終端&#xff0c;輸入&#xff1a; sudo apt-get updata 輸入root用戶密碼 2.更新完畢后&#xff0c;輸入 sudo apt-get install mysql-server ubuntu14.04安裝中間會讓你設置密碼&#xff0c;輸入密碼后點擊確認(mysql123) 3.安裝結束后&#xff0c;查看端口號是否開啟 …

Pthread創建線程后必須使用join或detach釋放線程資源

這兩天在看Pthread 資料的時候&#xff0c;無意中看到這樣一句話(man pthread_detach): Either pthread_join(3) or pthread_detach() should be called for each thread that an application creates, so that system resources for the thread can be released. …

二維數組求平均值(指針的使用)

#include<stdio.h>int main() {int buf[3][5] {{1,2,3,4,5},{4,5,6,7,8},{7,8,9,10,11}};int i;int j;//求行平均值 for(i 0; i < 3; i){int sum 0;for(j 0; j < 5; j){sum (*(*(buf i) j));}printf("sum %d\n",sum/5);}//求列平均值for(i 0; i …

linux終端關閉時為什么會導致在其上啟動的進程退出?

現象 經常在linux下開發的人應該都有這樣的經驗&#xff0c;就是在終端上啟動的程序&#xff0c;在關閉終端時&#xff0c;這個程序的進程也被一起關閉了。看下面這個程序&#xff0c;為了使進程永遠運行&#xff0c;在輸出helloworld后&#xff0c;循環調用sleep&#xff1a; …

二維數組做函數參數傳遞

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

libevent源碼深度剖析

第一章 1&#xff0c;前言 Libevent是一個輕量級的開源高性能網絡庫&#xff0c;使用者眾多&#xff0c;研究者更甚&#xff0c;相關文章也不少。寫這一系列文章的用意在于&#xff0c;一則分享心得&#xff1b;二則對libevent代碼和設計思想做系統的、更深層次的分析&#xff…

函數返回指針類型(strchr函數)

#include<stdio.h> #include<string.h> char *mystrchr(char *s,char c) {while(*s){if(*s c){return s;}s;}return NULL; }int main() {char str[100] "hello world";//char* s strchr(str,a);char *s mystrchr(str,e);//返回ello world字符串 prin…

函數與指針

#include<stdio.h>int add(int a,int b) {return ab; }int main() {void *p(int,char *); //聲明了一個函數 &#xff0c;函數名為p&#xff0c;函數返回值為void*,函數的 void (*p)(int,char *);//定義了一個指向參數為int和char*返回值為void的函數指針//定義一個參數為…

使用指針在函數中交換數值

#include<stdio.h>void swap(int* a,int *b) {/*int temp *a;*a * b;*b temp;*/*a *b;*b *a - *b;*a *a - *b; }int main() {int a 10;int b 20;swap(&a,&b);printf("a %d,b %d\n",a,b);} 轉載于:https://www.cnblogs.com/wanghao-boke/p/1…

linux C 基于鏈表鏈的定時器

源碼如下&#xff1a;util_timer.h#ifndef LST_TIMER#define LST_TIMER#include <time.h>#include <sys/time.h>#include <stdlib.h>#include <signal.h>#define BUFFER_SIZE 64struct util_timer;/*struct client_data{sockaddr_in address;int sockf…

libevent學習筆記 一、基礎知識

一、libevent是什么libevent是一個輕量級的開源的高性能的事件觸發的網絡庫&#xff0c;適用于windows、linux、bsd等多種平臺&#xff0c;內部使用select、epoll、kqueue等系統調用管理事件機制。它被眾多的開源項目使用&#xff0c;例如大名鼎鼎的memcached等。特點&#xff…

漢字逆置

在計算機中&#xff0c;一個漢字用無法用1個字節來表示 #include<stdio.h> int main() {char buf[256] "你好";int len 0;while(buf[len]);len--;printf("%d\n",len);// 4一個漢字兩個字節 //printf("%p\n",buf);return 0; } 在windows下…

libevent項目分析(一) -- 準備階段

項目的簡介 我理解libevent是一個輕量級的&#xff0c;跨平臺高效的&#xff08;C語言實現&#xff09;事件驅動庫&#xff0c;類似于ACE項目中的ACE_Reactor&#xff0c;它實現了網絡通訊套接口I/O事件&#xff0c;定時器事件&#xff0c;信號事件的監聽和事件處理函數回調機制…

混合字符串字符數統計

因為漢字占一個以上字節&#xff0c;如何統計一個既有漢字又有字母的字符串呢&#xff1f; 漢字在計算機中的ASCII是以負數來與其他普通字符的ASCII區分的。 #include<stdio.h> int main() {char buf[256] "你好世界";printf("%d\n",buf[0]); //-60…

清除字符串空格

1.清除字符串中右邊的空格 從字符串尾部開始&#xff0c;找到非空格處&#xff0c;將下一個字符置為0即可。 //清除右邊空格 #include<stdio.h> int main() {char buf[] "hello world ";int len 0;//calculate the length of stringwhile(buf[len]);le…