Makefile学习笔记
ls -l 查看文件详细信息
1.gcc -E test.c -o test.i
//预编译
gedit test.i //查看:高级C
2.gcc -Wall -S test.i -o test.s
//编译显示所有警告
gedit test.s //查看:介于C与汇编之间的
3. as test.s -o test.o
nano test.o//强行打开是乱码
一步到位:gcc -c test.c
gcc test.o
./a.out
five.c,five.h//新建
gcc -o test test.c five.c //编译为test
gedit Makefile
内容:
test: test.c five.c
gcc -o test.c five.c
clean:rm test
make clean
ls
make
ls
./test
修改其中一个文件,修改Makefile如下,改善编译
test: test.o five.o #源文件
TAB gcc -o test.o five.o #命令
test.o: test.c #源文件
TAB gcc -c test.c #命令
five.o: five.c #源文件
TAB gcc -c five.c #命令
clean:rm test #删除test
TAB rm *.o #删除所有.o
make clean
再次修改其中一个文件,修改Makefile如下,改善编译
方便以后修改调整程序
CC=gcc #像宏定义
CFLAGS=-c -Wall #像宏定义
#使用替换 $(。。。)
all:test
test: test.o five.o #源文件
TAB $(CC) -o test.o five.o #命令
test.o: test.c #源文件
TAB $(CC) $(CFLAGS) test.c #命令
five.o: five.c #源文件
TAB $(CC) $(CFLAGS) five.o #命令
clean:rm test #删除test
TAB rm *.o #删除所有.o
一个简单的makefile示例及其注释
相信在unix下编程的没有不知道makefile的,刚开始学习unix平台
下的东西,了解了下makefile的制作,觉得有点东西可以记录下。
下面是一个极其简单的例子:
现在我要编译一个Hello world,需要如下三个文件:
1. print.h
#include<stdio.h>
void printhello();
2. print.c
#include"print.h"
void printhello(){
printf("Hello, world\n");
}
3. main.c
#include "print.h"
int main(void){
printhello();
return 0;
}
好了,很简单的程序了。如果我们想要编译成功需要哪些步骤呢?
我认为在这里需要理解的就两步:
# 为每一个 *.c文件生成 *o文件。
# 连接每一个*o文件,生成可执行文件。
下面的makefile 就是根据这样的原则来写的。
一:makefile 雏形:
#makefile的撰写是基于规则的,当然这个规则也是很简单的,就是:
#target : prerequisites
command //任意的shell 命令
实例如下:
makefile:
helloworld : main.o print.o #helloword 就是我们要生成的目标
# main.o print.o是生成此目标的先决条件
gcc -o helloworld main.o print.o#shell命令,最前面的一定是一个tab键
mian.o : mian.c print.h
gcc -c main.c
print.o : print.c print.h
gcc -c print.c
clean :
rm helloworld main.o print.o
OK,一个简单的makefile制作完毕,现成我们输入 make,自动调用Gcc编译了,
输入 make clean就会删除 hellowworld mian.o print.o
二:小步改进:
在上面的例子中我们可以发现 main.o print.o 被定义了多处,
我们是不是可以向C语言中定义一个宏一样定义它呢?当然可以:
makefile:
objects = main.o print.o #应该叫变量的声明更合适
helloworld : $(objects) //声明了变量以后使用就要$()了
gcc -o helloworld$(objects)
mian.o : mian.c print.h
gcc -c main.c
print.o : print.c print.h
gcc -c print.c
clean :
rm helloworld $(objects)
修改完毕,这样使用了变量的话在很多文件的工程中就能体现出方便性了。
三:再进一步:
再看一下,为没一个*.o文件都写一句gcc -c main.c是不是显得多余了,
能不能把它干掉?而且 main.c 和print.c都需要print.h,为每一个都写上是
不是多余了,能不能再改进?
能,当然能了:
makefile:
objects = main.o print.o
helloworld : $(objects)
gcc -o helloworld$(objects)
$(objects) : print.h # 都依赖print.h
mian.o : mian.c #干掉了gcc -c main.c 让Gun make自动推导了。
print.o : print.c
clean :
rm helloworld $(objects)
好了,一个简单的makefile就这样完毕了,简单吧。
Makefile学习笔记的更多相关文章
- [转]Windows平台下Makefile学习笔记
Windows平台下Makefile学习笔记(一) 作者:朱金灿 来源:http://blog.csdn.net/clever101 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译 ...
- <转>Windows平台下Makefile学习笔记(二)
本文转自:http://blog.csdn.net/clever101/article/details/8286066 上次我们学习了怎么用Makefile编译一个控制台工程.这次我们学习一下如何使用 ...
- makefile学习笔记(多目录嵌套调用、变量使用)
http://blog.csdn.net/leexiang_han/article/details/9274229 学习了几天的makefile的嵌套调用编写也有一些心得,先声明,我也是初学者写文 ...
- makefile学习笔记(一)
1.1:make概述 在linux环境下使用make工具能够比较容易的构建一个属于自己的工程,整个工程的编译只需要一个命令就可以完成编译.连接以至于最后的执行.不过我们需要投入一些时间去学习如何完成m ...
- <转>Windows平台下Makefile学习笔记(一)
本文转自:http://blog.csdn.net/clever101/article/details/8147352 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译平台的问题(发 ...
- 【VS开发】Windows平台下Makefile学习笔记
作者:朱金灿 来源:http://blog.csdn.net/clever101 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译平台的问题(发现一些开源代码已经在使用VS2010开 ...
- makefile 学习笔记
1/ 编写简单makefile test_out: test.o g++ test.o -o test_out test.o: test.cpp test.h g++ -c test.cpp test ...
- Linux下Makefile学习笔记
makefile 可以用于编译和执行多个C/C++源文件和头文件. (1) #include "file.h" 和 #include <file.h> 的区别 #inc ...
- Makefile的学习笔记
Makefile的学习笔记 标签: makefilewildcard扩展includeshellfile 2012-01-03 00:07 9586人阅读 评论(2) 收藏 举报 分类: Linux ...
随机推荐
- 《基于MVC的Javascript》
var Class = function(){ var klass = function(){ this.init.apply(this,arguments); } klass.prototype.i ...
- linux如何修改主机名
一:#hostname hn1 二:修改/etc/sysconfig/network中的 HOSTNAME=hn1三:修改/etc/hosts文件 127.0.0.1 hn1
- Div样式查看器
编写div属性时,经常需要尝试不同的样式,可以用Javascript写一个简单的div样式查看器,方便日常操作: <!DOCTYPE html> <html> <head ...
- Generic泛型
1.问题 未使用泛型时,元素的类型不安全:操作麻烦,可能需要强制转换import java.util.ArrayList;import java.util.List;import org.junit. ...
- 328. Odd Even Linked List——多利用fake_head
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- lmdb简介——结合MVCC的B+树嵌入式数据库
lmdb简介 lmdb是openLDAP项目开发的嵌入式(作为一个库嵌入到宿主程序)存储引擎.其主要特性有: 基于文件映射IO(mmap) 基于B+树的key-value接口 基于MVCC(Multi ...
- __attribute__特性介绍以及变量和函数特定布局设置
ARM的MDK编译__attribute__介绍:http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348bc/Ciafc ...
- [转载]linux下svn常用指令
一下内容转载于:http://blog.chinaunix.net/space.php?uid=22976768&do=blog&id=1640924.这个总结的很好~ windows ...
- linux 安装 php
1. libxml2安装 tar -zxvf libxml2-2.6.26.tar.gz cd libxml2-2.6.26 ./configure --prefix=/usr ...
- iOS https(SSL/TLS)数据捕获
要捕获iPhone上的appstore的数据还真的没那么容易,以前介绍的那些使用代理手工导入证书的方法已经完全失效了,结果就是安装证书之后再打开appstore也无法正常的建立连接.按照我的分析其实是 ...