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.mzph.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…