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 ...
随机推荐
- 使用AndroidStudio编译NDK的方法及错误解决方案
参考资料: [android ndk]macos环境下Android Studio中利用gradle编译jni模块及配置:http://demo.netfoucs.com/ashqal/article ...
- Windows下安装并设置Redis
Redis对于Linux是官方支持的,安装和使用没有什么好说的,普通使用按照官方指导,5分钟以内就能搞定.详情请参考: http://redis.io/download 但有时候又想在windows下 ...
- 用excel处理重复数据
我们在处理数据时,重复数据常常会对分析造成很大麻烦,因此数据整理初期一个重要的工作是排重,excel2007以上版本中有一个删除重复项功常便捷,但是每次点来点去也很麻烦,下面我们用公式来对一些重复数据 ...
- 小程序---根据数据库反向生成java文件
工作中写entry太繁琐,写了一个小程序反向生成.从而大大减少了工作量 import java.io.File; import java.io.FileWriter; import java.io.I ...
- hdu----(5055)Bob and math problem(贪心)
Bob and math problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- C语言实现进制转换
#include<stdio.h> int main() { char ku[16]={'0','1','2','3','4','5','6','7','8','9','A','B ...
- 如何做好一位资深的web前端工程师
Web前端开发工程师是一个很新的职业,在国内乃至国际上真正开始受到重视的时间不超过5年.Web前端开发是从网页制作演变而来的,名称上有很明 显的时代特征.在互联网的演化进程中,网页制作是Web 1.0 ...
- ASP.NET Web API路由规
默认的规则 在ASP.NET MVC4中 global.asax.cs代码中并无注册默认路由规则的代码 代码如下: public class WebApiApplication : System.We ...
- windows系统下Tomcat与Apache服务器集成
说明:此文是看书真实试验成功的,书中提到了不同版本不兼容的问题,但是很荣幸我没碰到,此例可供参考. 本文假设你已经有了java环境和tomcat,你已经熟悉tomcat的应用. Jdk 1.7.0_5 ...
- linux命令行快捷键
linux命令行编辑快捷键 先总结几个个人觉得最有用的 ctrl + ? 撤消前一次输入 ctrl + c 另起一行 ctrl + r 输入单词搜索历史命令 ctrl + u 删除光标前面所有字符相当 ...