转载:Linux下简单makefile编写示例
2016-02-24 15:04
477 查看
from:/article/1797612.html
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
对于程序设计员来说,makefile是我们绕不过去的一个坎。可能对于习惯Visual C++的用户来说,是否会编写makefile无所谓。毕竟工具本身已经帮我们做好了全部的编译流程。但是在Linux上面,一切变得不一样了,没有人会为你做这一切。编代码要靠你,测试要靠你,最后自动化编译设计也要靠你自己。想想看,如果你下载了一个开源软件,却因为自动化编译失败,那将会在很大程度上打击你学习代码的自信心了。所以,我的理解是这样的。我们要学会编写makefile,至少会编写最简单的makefile。
首先编写add.c文件,
[cpp] view plain copy
#include "test.h"
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
printf(" 2 + 3 = %d\n", add(2, 3));
printf(" 2 - 3 = %d\n", sub(2, 3));
return 1;
}
再编写sub.c文件,
[cpp] view plain copy
#include "test.h"
int sub(int a, int b)
{
return a - b;
}
最后编写test.h文件,
[cpp] view plain copy
#ifndef _TEST_H
#define _TEST_H
int add(int a, int b);
int sub(int a, int b);
#endif
那么,就是这三个简单的文件,应该怎么编写makefile呢?
[cpp] view plain copy
test: add.o sub.o
gcc -o test add.o sub.o
add.o: add.c test.h
gcc -c add.c
sub.o: sub.c test.h
gcc -c sub.c
clean:
rm -rf test
rm -rf *.o
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
对于程序设计员来说,makefile是我们绕不过去的一个坎。可能对于习惯Visual C++的用户来说,是否会编写makefile无所谓。毕竟工具本身已经帮我们做好了全部的编译流程。但是在Linux上面,一切变得不一样了,没有人会为你做这一切。编代码要靠你,测试要靠你,最后自动化编译设计也要靠你自己。想想看,如果你下载了一个开源软件,却因为自动化编译失败,那将会在很大程度上打击你学习代码的自信心了。所以,我的理解是这样的。我们要学会编写makefile,至少会编写最简单的makefile。
首先编写add.c文件,
[cpp] view plain copy
#include "test.h"
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
printf(" 2 + 3 = %d\n", add(2, 3));
printf(" 2 - 3 = %d\n", sub(2, 3));
return 1;
}
再编写sub.c文件,
[cpp] view plain copy
#include "test.h"
int sub(int a, int b)
{
return a - b;
}
最后编写test.h文件,
[cpp] view plain copy
#ifndef _TEST_H
#define _TEST_H
int add(int a, int b);
int sub(int a, int b);
#endif
那么,就是这三个简单的文件,应该怎么编写makefile呢?
[cpp] view plain copy
test: add.o sub.o
gcc -o test add.o sub.o
add.o: add.c test.h
gcc -c add.c
sub.o: sub.c test.h
gcc -c sub.c
clean:
rm -rf test
rm -rf *.o
相关文章推荐
- MYSQL5.5源码安装 linux下
- linux练习题01
- (转)centos6.5安装VNC
- 转载:Linux下C动态库与静态库使用示例
- centos7.0 阿里巴巴学生服务器搭配环境实录(1)--java1.8环境搭建
- linux服务器安装jdk
- Linux功能的部分模拟
- CentOS 关闭virbr0
- Linux at命令
- 监控 Linux 性能的 18 个命令行工具[转]
- 4个强大的Linux服务器监控工具[转]
- Linux启动过程详解
- centos7刚配置完遇到的部分问题
- centos 防火墙设置随手记
- 转载:Linux 关于动态链接库以及静态链接库的一些概念
- CentOS下防火墙的基本操作命令
- Java 开发必会的 Linux 命令
- Linux下gcc安装与使用(常用的gcc命令)
- 常用的Linux指令+Android下的Linux指令
- 查看linux服务器CPU详细情况