免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1985 | 回复: 4
打印 上一主题 下一主题

[Linux] 这么简单一个Makefile文件,怎么总是报错? [复制链接]

论坛徽章:
5
程序设计版块每日发帖之星
日期:2016-06-01 06:20:00数据库技术版块每日发帖之星
日期:2016-06-11 06:20:00操作系统版块每日发帖之星
日期:2016-06-13 06:20:00数据库技术版块每日发帖之星
日期:2016-06-13 06:20:00数据库技术版块每日发帖之星
日期:2016-08-07 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-09-27 15:45 |只看该作者 |倒序浏览
  1. [test@hadoop-node1 test]$ cat hellofunc.c
  2. #include <stdio.h>
  3. #include <hellomake.h>

  4. void myPrintHelloMake(void) {

  5.   printf("Hello makefiles!\n");

  6.   return;
  7. }
  8. [test@hadoop-node1 test]$ cat hellomake.c
  9. #include <hellomake.h>

  10. int main() {
  11.   // call a function in another file
  12.      myPrintHelloMake();

  13.        return(0);
  14.        }
  15. [test@hadoop-node1 test]$ cat hellomake.h
  16. /*
  17. example include file
  18. */

  19. void myPrintHelloMake(void);
  20. [test@hadoop-node1 test]$ cat Makefile
  21. hellomake: hellomake.o hellofunc.o
  22.         gcc -o hellomake hellomake.o hellofunc.o

  23. hellomake.o: hellomake.c hellomake.h
  24.         gcc -c hellomake.c

  25. hellofunc.o: hellofunc.c hellomake.h stdio.h
  26.         gcc -c hellofun.c

  27. .PHONY:clean
  28. clean:
  29.         rm hellomake
  30. [test@hadoop-node1 test]$ make
  31. gcc -c hellomake.c
  32. hellomake.c:1:23: error: hellomake.h: No such file or directory
  33. make: *** [hellomake.o] Error 1
  34. [test@hadoop-node1 test]$

复制代码
感觉,这已经算是最简单的一个Makefile文件了,为什么make的时候总是有问题呢?

谢谢

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2016-09-27 15:59 |只看该作者
本帖最后由 MMMIX 于 2016-09-27 16:14 编辑

回复 1# cqlouis

为什么make的时候总是有问题呢?

因为你调 gcc 的时候出问题了呀:

gcc -c hellomake.c
hellomake.c:1:23: error: hellomake.h: No such file or directory


这个 gcc 的错误信息你怎么就视而不见呢?

正确的用法是

gcc -L. -c hellomake.c

另外,包含项目特有的头文件的时候最好用 #include "...",而不是 #include <...>,后面这个是为系统头文件准备的。

另外 again,把头文件包含语句改过来之后,就可以不用 -L. 而直接用 gcc -c 编译了。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2016-09-27 16:18 |只看该作者
回复 1# cqlouis

代码本身和组织方式都有些问题,可以参考如下修改过的版本:
hellofunc.c

  1. #include <stdio.h>
  2. #include "hellofunc.h"

  3. void myPrintHelloMake(void) {
  4.         printf("Hello makefiles!\n");
  5. }

复制代码


对应的头文件 hellofunc.h:

  1. #ifndef HELLOMAKE_H
  2. #define HELLOMAKE_H

  3. void myPrintHelloMake(void);

  4. #endif /* HELLOMAKE_H */

复制代码


主程序 hellomake.c

  1. #include "hellofunc.h"

  2. int main() {
  3.         // call a function in another file
  4.         myPrintHelloMake();
  5.         return 0;
  6. }

复制代码


Makefile

  1. CC = gcc


  2. all: hellomake

  3. hellomake: hellomake.o hellofunc.o

  4. hellofunc.o: hellofunc.c hellofunc.h

  5. clean:
  6.         $(RM) *.o hellomake

复制代码


编译链接部分只需要提供依赖即可,具体规则使用 Make 默认的就行了。

论坛徽章:
5
程序设计版块每日发帖之星
日期:2016-06-01 06:20:00数据库技术版块每日发帖之星
日期:2016-06-11 06:20:00操作系统版块每日发帖之星
日期:2016-06-13 06:20:00数据库技术版块每日发帖之星
日期:2016-06-13 06:20:00数据库技术版块每日发帖之星
日期:2016-08-07 06:20:00
4 [报告]
发表于 2016-09-28 09:18 |只看该作者
回复 3# MMMIX
首先谢谢你的耐心解答,非常感谢!

不过还有疑问:

hellofunc.o: hellofunc.c hellofunc.h

这句,为什么是不是:

hellofunc.o: hellofunc.c hellofunc.h  stdio.h 呢?


stdio.h这个头文件为什么不放进去呢?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
5 [报告]
发表于 2016-09-28 09:24 |只看该作者
回复 4# cqlouis

stdio.h这个头文件为什么不放进去呢?


系统头文件呀,通常是不变的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP