linux下的C语言开发(makefile编写)


     转地址: http://blog.csdn.net/feixiaoxing/article/details/7197095

linux下的C语言开发(makefile编写)

标签: makefilelinux语言c工具测试
22628人阅读 评论(11) 收藏 举报
分类:
【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

    对于程序设计员来说,makefile是我们绕不过去的一个坎。可能对于习惯Visual C++的用户来说,是否会编写makefile无所谓。毕竟工具本身已经帮我们做好了全部的编译流程。但是在Linux上面,一切变得不一样了,没有人会为你做这一切。编代码要靠你,测试要靠你,最后自动化编译设计也要靠你自己。想想看,如果你下载了一个开源软件,却因为自动化编译失败,那将会在很大程度上打击你学习代码的自信心了。所以,我的理解是这样的。我们要学会编写makefile,至少会编写最简单的makefile。

    首先编写add.c文件,

  1. #include "test.h"  
  2. #include <stdio.h>  
  3.   
  4. int add(int a, int b)  
  5. {  
  6.     return a + b;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     printf(" 2 + 3 = %d\n", add(2, 3));  
  12.     printf(" 2 - 3 = %d\n", sub(2, 3));  
  13.     return 1;  
  14. }  
#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文件,

  1. #include "test.h"  
  2.   
  3. int sub(int a, int b)  
  4. {  
  5.     return a - b;  
  6. }  
#include "test.h"

int sub(int a, int b)
{
    return a - b;
}
    最后编写test.h文件,

  1. #ifndef _TEST_H  
  2. #define _TEST_H  
  3.   
  4. int add(int a, int b);  
  5. int sub(int a, int b);  
  6. #endif  
#ifndef _TEST_H
#define _TEST_H

int add(int a, int b);
int sub(int a, int b);
#endif
    那么,就是这三个简单的文件,应该怎么编写makefile呢?

  1. test: add.o sub.o  
  2.     gcc -o test add.o sub.o  
  3.   
  4. add.o: add.c test.h  
  5.     gcc -c add.c  
  6.   
  7. sub.o: sub.c test.h  
  8.     gcc -c sub.c      
  9.       
  10. clean:  
  11.     rm -rf test  
  12.     rm -rf *.o  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值