Makefile的制作
一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,makefile定义系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makefile就想一个Shell脚本一样,其中也可以执操作系统的命令。
Linux环境下的程序员如果不会使用GUN make来构建和管理自己的工程,应该不能算是一个合格的专业程序员。在Linux(Unix)环境下使用GUN的make工具能够比较容易的构建一个属于你自己的工程,整个工程的编译只需要一个命令就可以完成编译、连接以至于最后的执行。不过这需要我们投入一些时间去完成一个或者多个称之为makefile文件的编写。
所要完成的makefile文件描述了整个工程的编译、连接等规则。其中包括:工程的哪些源文件需要编译、需要创建哪些库文件以及如何创建这些库文件、如何最后产生我们想要的可执行文件。尽管看起来可能是很复杂的事情,但是为了工程编写makefile的好处是能够使用一行命令来完成“自动化编译”,一旦提供一个正确的makefile。编译整个工程你嗦要做的唯一的一件事就是在shell提示符下输入make命令,这个工程完全自动编译,极大地提高了效率。
Makefile结构
#表示注释
变量定义
VAR=test 定义变量VAR,强制付志伟test
VAR+=app 在VAR之前定义的值后面再追加app这个值
VAR?=testapp 如果之前VAR没有被定义,则定义并使用testapp,否则使用之前的值
第一条目标为总的目标
依赖可以是文件(目录)或为其他目标
动作可以是Linux命令,动作的哪一行第一个字符必须是以TAB键补齐的
target: depend1 depend2 depend3 ......
[TAB] action1
[TAB] action2
target:
[TAB] action1
[TAB] action2
makefile的使用
make找makefile或者Makefile文件执行总的目标
make clean 执行makefile文件中的clean目标
make -C directory 进入到directory文件夹中去执行总的目标
make clean -c directory 进入到directory文件夹中去执行clean目标
make -f comm_makefile 通过-f选项指定一个makefile文件
make VAR=value 给makefile传一个参数VAR,其值为value
案例:
分别创建vendor1与vendor2两个文件夹,在这两个文件夹中放两个函数
[xiaohexiansheng@centos6 library]$ mkdir vendor1
[xiaohexiansheng@centos6 library]$ mkdir vendor2
[xiaohexiansheng@centos6 library]$ cd vendor1
[xiaohexiansheng@centos6 vendor1]$ vim crypto.c
#include <stdio.h> void crypto(void)
{
printf("Start crypt..."); return ;
}
[xiaohexiansheng@centos6 vendor1]$ vim crypto.h
#ifndef _CRYPTO_H_
#define _CRYPTO_H_ void crypto(void); #endif
[xiaohexiansheng@centos6 vendor1]$ vim makefile
LIB_NAME=vendor1 all: shared static
rm -f *.o shared:
gcc -shared -fpic -o lib${LIB_NAME}.so *.c static:
gcc -c *.c
ar -rcs lib${LIB_NAME}.a *.o clean:
rm -f *.a *.so
rm -f *.o install:
cp lib${LIB_NAME}.* ../libs
[xiaohexiansheng@centos6 vendor1]$ cd ../vendor2
[xiaohexiansheng@centos6 vendor2]$ vim func.c
#include <stdio.h> void func(void)
{
printf("Start func..."); return ;
}
[xiaohexiansheng@centos6 vendor2]$ vim func.h
#ifndef _FUNC_H_
#define _FUNC_H_ void func(void); #endif
[xiaohexiansheng@centos6 vendor2]$ vim makefile
LIB_NAME=vendor2 all: shared static
rm -f *.o shared:
gcc -shared -fpic -o lib${LIB_NAME}.so *.c static:
gcc -c *.c
ar -rcs lib${LIB_NAME}.a *.o clean:
rm -f *.a *.so
rm -f *.o install:
cp lib${LIB_NAME}.* ../libs
[xiaohexiansheng@centos6 vendor2]$ cd ..
[xiaohexiansheng@centos6 library]$ vim main.c
#include "crypto.h"
#include "func.h" int main(void)
{
crypto();
func(); return ;
}
[xiaohexiansheng@centos6 library]$ vim makefile
APP_NAME?=app all: lib_vendor1 lib_vendor2
gcc -static main.c -Ivendor1 -Ivendor2 -o ${APP_NAME} -Lvendor1 -Lvendor2 -lvendor1 -lvendor2 lib_vendor1:
make -C vendor1 lib_vendor2:
make -C vendor2
[xiaohexiansheng@centos6 library]$ ls
main.c makefile vendor1 vendor2
[xiaohexiansheng@centos6 library]$ make
make -C vendor1
make[1]: Entering directory `/home/xiaohexiansheng/cc/library/vendor1'
gcc -shared -fpic -o libvendor1.so *.c
gcc -c *.c
ar -rcs libvendor1.a *.o
rm -f *.o
make[1]: Leaving directory `/home/xiaohexiansheng/cc/library/vendor1'
make -C vendor2
make[1]: Entering directory `/home/xiaohexiansheng/cc/library/vendor2'
gcc -shared -fpic -o libvendor2.so *.c
gcc -c *.c
ar -rcs libvendor2.a *.o
rm -f *.o
make[1]: Leaving directory `/home/xiaohexiansheng/cc/library/vendor2'
gcc main.c -Ivendor1 -Ivendor2 -o app -Lvendor1 -Lvendor2 -lvendor1 -lvendor2
[xiaohexiansheng@centos6 library]$ ls
app main.c makefile vendor1 vendor2
Makefile的制作的更多相关文章
- makefile文件制作入门
一.首先,看一下最简单的C文件 //hello.c文件 #include <stdio.h> void main() { printf("hello world\n") ...
- C语言Makefile文件制作
本文摘抄自“跟我一起写Makefile ”,只是原文中我自己感觉比较精要的一部分,并且只针对C语言,使用GCC编译器. 原文请看这里:http://wiki.ubuntu.org.cn/%E8%B7% ...
- 第三课 Makefile文件的制作(上)
1.序言: 前面的课程讲解了从gcc编译过程到其实践,大家可以看到其实在这些步骤中有些是可以简化编译的,但由于参数多以及项目中文件数量多的原因难免会造成错误甚至是浪费大量的时间在这编译上,为此linu ...
- Makefile学习笔记
ls -l 查看文件详细信息 1.gcc -E test.c -o test.i//预编译gedit test.i //查看:高级C 2.gcc -Wall -S test.i -o test.s// ...
- kernel Makefile Kconfig说明
实际文档位置:Documentation/kbuild/makefiles.txt,此为翻译稿. *************************************************** ...
- makefile编写helloworld
相信在unix下编程的没有不知道makefile的,刚开始学习unix平台 下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其简单的例子: 现在我要编译一个Hello ...
- 通过busybox制作根文件系统
通过busybox制作根文件系统可以自定义选项,在制作的根文件系统中添加需要的命令,指定生成的根文件系统到相应的目录下. 一. 根文件系统的获取方式--->官网: https://busybox ...
- 简单makefile示例
Makefile cmd: - g++ 相信在linux下编程的没有不知道makefile的,刚开始学习linux平台下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其 ...
- u-boot移植总结(四)u-boot-2010.09框架分析
(一)本次移植是基于FL2440,板子的基本硬件: CPU 型号为S3C2440,基于ARM920T,指令集ARMV4,时钟主频400MHz SDRAM H57V2562GTR-75C 2片*32MB ...
随机推荐
- camera驱动框架分析(下)
sensor的驱动 v4l2_i2c_new_subdev_board先用client = i2c_new_device(adapter, info);创建info对应的i2c_client对象(代表 ...
- 【Linux驱动学习】SD卡规范学习
摘要: 学习SD卡的相关规范,包括定义,硬件特性,数据传输,命令系统等.不涉及代码. 文章针对Linux驱动开发而写,以助于理解SD卡驱动,不会涉及过多硬件内容. 纲要: 1. SD卡介绍 2. SD ...
- 一次向svn中增加所有新增文件 svn add all new files【转】
以下摘自:<卓有成效的程序员>之自动化 转自:http://blog.csdn.net/spare_h/article/details/6677435 我经常会一次往Subversion里 ...
- Oracle rman 全备份的一个小例子
run{ allocate channel d1 type disk; backup database format='/u01/backup/%T_%d_%s.bak'; sql 'alter ...
- 无法解析的DNS服务地址
如果DNS服务器地址设置不当,可能会导致网速慢.出现弹窗广告.网址打不开.打开不是自己想要的网站等一系列问题. 请参考: DNS的作用是什么,怎样设置DNS? https://jingyan.baid ...
- Android之Activity
Activity总结: Activity的顶层View是DecorView,而我们在onCreate函数中通过setContentView设置的View只不过是这个DecorView的一部分罢了.De ...
- python的上下文管理(context)(1)
本文转载自:http://blog.csdn.net/G_66_hero/article/details/53048540 什么是Python中的上下文管理器 怎么使用上下文管理器 如何创建自己的上下 ...
- 创建ProcessEngine
activiti流程引擎是通过activiti.cfg.xml文件配置的(这并不符合Spring构建流程引擎的编码风格). ProcessEngine processEngine = ProcessE ...
- Django基础之模板
Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }} 和 {% %} 变量相关的用{{ }},逻辑相关的用{% %}. 变量 {{ 变量名 }} 变量名由字母数字和下划线组成 ...
- ubuntu 16.04 qtcreator 闪退
当用QtCreator 进行代码自动补全时,比如编写ros代码,ROS_INFO时候就会出现闪退,后面按照 http://doc.qt.io/qtcreator/creator-clang-codem ...