关于linux中Makefile文件的编写(简单例子)
来源:互联网 发布:捷安特淘宝有卖吗 编辑:程序博客网 时间:2024/06/11 18:30
首先创建两个.c文件和一个.h文件,这样可以简单的模拟一个项目源文件
file1.c
#include <stdio.h>
#include "file2.h"
int main()
{
printf("print file1");
File2Print();
return 0;
}
file2.h
#ifndef FILE2_H_
#define FILE2_H_
#ifdef __cplusplus
extern "C" {
#endif
void File2Print();
#ifdef __cplusplus
}
#endif
#endif
file2.c
#include "file2.h"
void File2Print()
{
printf("Print file2");
}
然后开始在该源文件存放的地方创建Makefile文件(文件命名可以随意)
helloworld:file1.o file2.o
gcc file1.o file2.o -o helloworld
file1.o:file1.c file2.h
gcc -c file1.c -o file1.o
file2.o:file2.c file2.h
gcc -c file2.c -o file2.o
clean:
rm -rf *.o helloworld
注意:这边所有命令前面空开的地方都是tab键而不能是空格
首先解释下
1、helloworld和clean是我们自己定义的命令,只需要在linux下执行make helloworld/clean就可以执行makefile中自己定义的操作
2、helloworld:file1.o file2.o表示helloworld是依赖于file1.o,file2.o的
makefile进阶
实例:
项目文件:
-rw-r--r-- 1 tgomc tgomc 1144 Oct 9 14:35 Distinguish.cpp
-rw-r--r-- 1 tgomc tgomc 346 Oct 9 14:25 makefile
-rw-r--r-- 1 tgomc tgomc 3131 Oct 9 14:18 Pmap.cpp
-rw-r--r-- 1 tgomc tgomc 672 Oct 9 11:12 Pmap.h
-rw-r--r-- 1 tgomc tgomc 436 Oct 9 11:12 Read.cpp
-rw-r--r-- 1 tgomc tgomc 497 Oct 9 11:12 Read.h
-rw-r--r-- 1 tgomc tgomc 1204 Oct 9 11:12 ReadMe.txt
-rw-r--r-- 1 tgomc tgomc 290 Oct 9 11:12 StdAfx.cpp
-rw-r--r-- 1 tgomc tgomc 746 Oct 9 11:12 StdAfx.h
-rw-r--r-- 1 tgomc tgomc 201 Oct 9 14:37 test.cfg
-rw-r--r-- 1 tgomc tgomc 2712 Oct 9 14:19 Write.cpp
-rw-r--r-- 1 tgomc tgomc 915 Oct 9 11:12 Write.h
makefile内容:
objects=Distinguish.o Pmap.o Write.o
Dis:$(objects)
g++ $(objects) -o Dis
Distinguish.o:Distinguish.cpp Write.h Pmap.h StdAfx.h
g++ -c Distinguish.cpp -o Distinguish.o
Pmap.o:Pmap.cpp Pmap.h Write.h StdAfx.h
g++ -c Pmap.cpp -o Pmap.o
Write.o:Write.cpp Write.h Pmap.h StdAfx.h
g++ -c Write.cpp -o Write.o
clean:
rm Dis $(objects)
make Dis之后的文件目录:
-rwxrwxrwx 1 tgomc tgomc 58658 Oct 9 14:42 Dis
-rw-r--r-- 1 tgomc tgomc 1144 Oct 9 14:35 Distinguish.cpp
-rw-rw-rw- 1 tgomc tgomc 35800 Oct 9 14:42 Distinguish.o
-rw-r--r-- 1 tgomc tgomc 346 Oct 9 14:25 makefile
-rw-r--r-- 1 tgomc tgomc 3131 Oct 9 14:18 Pmap.cpp
-rw-r--r-- 1 tgomc tgomc 672 Oct 9 11:12 Pmap.h
-rw-rw-rw- 1 tgomc tgomc 50768 Oct 9 14:42 Pmap.o
-rw-r--r-- 1 tgomc tgomc 436 Oct 9 11:12 Read.cpp
-rw-r--r-- 1 tgomc tgomc 497 Oct 9 11:12 Read.h
-rw-r--r-- 1 tgomc tgomc 1204 Oct 9 11:12 ReadMe.txt
-rw-r--r-- 1 tgomc tgomc 290 Oct 9 11:12 StdAfx.cpp
-rw-r--r-- 1 tgomc tgomc 746 Oct 9 11:12 StdAfx.h
-rw-r--r-- 1 tgomc tgomc 201 Oct 9 14:37 test.cfg
-rw-r--r-- 1 tgomc tgomc 2712 Oct 9 14:19 Write.cpp
-rw-r--r-- 1 tgomc tgomc 915 Oct 9 11:12 Write.h
-rw-rw-rw- 1 tgomc tgomc 46464 Oct 9 14:42 Write.o
可以运行./Dis执行项目
- 关于linux中Makefile文件的编写(简单例子)
- makefile编写的简单例子
- Linux下简单Makefile文件的编写
- 【linux】——Linux下简单makefile文件的编写
- 编写简单的Makefile文件
- 简单的Makefile文件编写
- Linux-简单的Makefile编写
- 嵌入式驱动关于Makefile的编写例子
- linux下编写的一个最简单的Makefile文件
- linux中Makefile的编写
- 编写Makefile简单文件
- Linux相关-------简单工程Makefile的编写
- linux makefile文件编写
- 【Linux】makefile文件编写
- linux 编写makefile文件
- 关于erlang中makefile的编写
- linux设备驱动程序的makefile文件例子
- Linux设备驱动程序的Makefile文件例子
- 制作临时证书为ActiveX控件签名
- 安徽省高中继续教育点击助手
- dspace批量导入编码问题
- 转:VC MFC菜单栏(CMenu)控件(菜单栏字体大小的更改。。。等等)
- CAS 导入导出 密钥
- 关于linux中Makefile文件的编写(简单例子)
- css margin:0; padding:0;
- 人际关系经验
- 安全知识:公司防火墙应做的10件事
- gma950 调试 xna4.0代码 编译正常 运行出错
- 序列化与json
- Android software 的级别定位
- OpenMax多媒体引擎
- qsort