makefile中如何包含不同目录下的问题

oxionghui 2015-03-09 10:05:29
最近正在学makefile,但是一上来就遇到了一个问题。求各位大神指点。。。

情况如下:
pro文件夹下有:src目录,include目录,makefile文件。
其中:
src目录下有testClass.c文件,内容如下:
#include <stdio.h>
#include "help.h"
int main()
{
printf("hello wolrd\n");
printf("sum=%d\n", sum(2,3));
return 0;
}

include目录下有help.h文件,内容如下:
int sum(int a, int b)
{
return a + b;
}

makefile内容如下:
VPATH=include:src
vpath %.c src
vpath %.h inclue

main: testClass.o
gcc -o main testClass.o

testClass.o: help.h
. gcc -c testClass.c


make的时候老是找不到文件,求帮忙指点下这个makefile要怎么写,最好可以写个可以用的makefile文件,并加上说明,学习学习。不胜感激。。。。
...全文
641 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
program2050 2015-06-28
  • 打赏
  • 举报
回复
使用cmake,不用再担心不会写Makefile了 楼主试试cmake吧。轻松很多,这样就不用辛苦的写Makefile了。
zhxianbin 2015-03-10
  • 打赏
  • 举报
回复
testClass.o: help.h h文件不要写入依赖,怎么写可以看看 《跟我一起写makefile》 可以 Google 通用 makefile 看看
取啥都被占用 2015-03-10
  • 打赏
  • 举报
回复
引用 楼主 oxionghui 的回复:
最近正在学makefile,但是一上来就遇到了一个问题。求各位大神指点。。。 情况如下: pro文件夹下有:src目录,include目录,makefile文件。 其中: src目录下有testClass.c文件,内容如下: #include <stdio.h> #include "help.h" int main() { printf("hello wolrd\n"); printf("sum=%d\n", sum(2,3)); return 0; } include目录下有help.h文件,内容如下: int sum(int a, int b) { return a + b; } makefile内容如下: VPATH=include:src vpath %.c src vpath %.h inclue main: testClass.o gcc -o main testClass.o testClass.o: help.h . gcc -c testClass.c make的时候老是找不到文件,求帮忙指点下这个makefile要怎么写,最好可以写个可以用的makefile文件,并加上说明,学习学习。不胜感激。。。。
楼主的make 好高级。我的make很土...
test: add.o sub.o    #test是最后生成的文件,由add.o和sub.o合成
    gcc -o test add.o sub.o    
    
add.o: add.c test.h    #add.o由add.c 和 test.h合成编译生成。如果文件们的路径不同就在前面加个路径试试。反正add.o会在当前目录生成出来,就ok嘛。
    gcc -c add.c    
    
sub.o: sub.c test.h    #同上
    gcc -c sub.c        
        
clean:    
    rm -rf test    
    rm -rf *.o  
我也是尝试回答。楼主找到解决方案的话post出来 让大家一起学习一下吧。谢谢。
oxionghui 2015-03-10
  • 打赏
  • 举报
回复
引用 5 楼 oxionghui 的回复:
[quote=引用 2 楼 u011410413 的回复:] [quote=引用 楼主 oxionghui 的回复:] 最近正在学makefile,但是一上来就遇到了一个问题。求各位大神指点。。。 情况如下: pro文件夹下有:src目录,include目录,makefile文件。 其中: src目录下有testClass.c文件,内容如下: #include <stdio.h> #include "help.h" int main() { printf("hello wolrd\n"); printf("sum=%d\n", sum(2,3)); return 0; } include目录下有help.h文件,内容如下: int sum(int a, int b) { return a + b; } makefile内容如下: VPATH=include:src vpath %.c src vpath %.h inclue main: testClass.o gcc -o main testClass.o testClass.o: help.h . gcc -c testClass.c make的时候老是找不到文件,求帮忙指点下这个makefile要怎么写,最好可以写个可以用的makefile文件,并加上说明,学习学习。不胜感激。。。。
楼主的make 好高级。我的make很土...
test: add.o sub.o    #test是最后生成的文件,由add.o和sub.o合成
    gcc -o test add.o sub.o    
    
add.o: add.c test.h    #add.o由add.c 和 test.h合成编译生成。如果文件们的路径不同就在前面加个路径试试。反正add.o会在当前目录生成出来,就ok嘛。
    gcc -c add.c    
    
sub.o: sub.c test.h    #同上
    gcc -c sub.c        
        
clean:    
    rm -rf test    
    rm -rf *.o  
我也是尝试回答。楼主找到解决方案的话post出来 让大家一起学习一下吧。谢谢。[/quote]: 好的,大家一起学习,我也是边学边边写着试试的。[/quote]
引用 2 楼 u011410413 的回复:
[quote=引用 楼主 oxionghui 的回复:] 最近正在学makefile,但是一上来就遇到了一个问题。求各位大神指点。。。 情况如下: pro文件夹下有:src目录,include目录,makefile文件。 其中: src目录下有testClass.c文件,内容如下: #include <stdio.h> #include "help.h" int main() { printf("hello wolrd\n"); printf("sum=%d\n", sum(2,3)); return 0; } include目录下有help.h文件,内容如下: int sum(int a, int b) { return a + b; } makefile内容如下: VPATH=include:src vpath %.c src vpath %.h inclue main: testClass.o gcc -o main testClass.o testClass.o: help.h . gcc -c testClass.c make的时候老是找不到文件,求帮忙指点下这个makefile要怎么写,最好可以写个可以用的makefile文件,并加上说明,学习学习。不胜感激。。。。
楼主的make 好高级。我的make很土...
test: add.o sub.o    #test是最后生成的文件,由add.o和sub.o合成
    gcc -o test add.o sub.o    
    
add.o: add.c test.h    #add.o由add.c 和 test.h合成编译生成。如果文件们的路径不同就在前面加个路径试试。反正add.o会在当前目录生成出来,就ok嘛。
    gcc -c add.c    
    
sub.o: sub.c test.h    #同上
    gcc -c sub.c        
        
clean:    
    rm -rf test    
    rm -rf *.o  
我也是尝试回答。楼主找到解决方案的话post出来 让大家一起学习一下吧。谢谢。[/quote] 你这样全部放在一个文件夹里面,肯定是没有问题的,我其实是故意把文件放在不同目录,因为如果是真正的大型项目,代码源文件分类会比较好。我这个例子就是想测试下,放到不同目录下,如何处理的。上网查了下,但是自己跟着做的时候,还是有些问题。
oxionghui 2015-03-10
  • 打赏
  • 举报
回复
引用 2 楼 u011410413 的回复:
[quote=引用 楼主 oxionghui 的回复:] 最近正在学makefile,但是一上来就遇到了一个问题。求各位大神指点。。。 情况如下: pro文件夹下有:src目录,include目录,makefile文件。 其中: src目录下有testClass.c文件,内容如下: #include <stdio.h> #include "help.h" int main() { printf("hello wolrd\n"); printf("sum=%d\n", sum(2,3)); return 0; } include目录下有help.h文件,内容如下: int sum(int a, int b) { return a + b; } makefile内容如下: VPATH=include:src vpath %.c src vpath %.h inclue main: testClass.o gcc -o main testClass.o testClass.o: help.h . gcc -c testClass.c make的时候老是找不到文件,求帮忙指点下这个makefile要怎么写,最好可以写个可以用的makefile文件,并加上说明,学习学习。不胜感激。。。。
楼主的make 好高级。我的make很土...
test: add.o sub.o    #test是最后生成的文件,由add.o和sub.o合成
    gcc -o test add.o sub.o    
    
add.o: add.c test.h    #add.o由add.c 和 test.h合成编译生成。如果文件们的路径不同就在前面加个路径试试。反正add.o会在当前目录生成出来,就ok嘛。
    gcc -c add.c    
    
sub.o: sub.c test.h    #同上
    gcc -c sub.c        
        
clean:    
    rm -rf test    
    rm -rf *.o  
我也是尝试回答。楼主找到解决方案的话post出来 让大家一起学习一下吧。谢谢。[/quote]: 好的,大家一起学习,我也是边学边边写着试试的。
oxionghui 2015-03-10
  • 打赏
  • 举报
回复
引用 3 楼 zhxianbin 的回复:
testClass.o: help.h h文件不要写入依赖,怎么写可以看看 《跟我一起写makefile》 可以 Google 通用 makefile 看看
: 我也是看这篇文章去学习makefile的,但是我还没有看完,我看了一些,然后就想写个例子试试,然后就碰到上面的问题了。我回头继续看。多谢了。
奔跑的路 2015-03-09
  • 打赏
  • 举报
回复
pro---src---testClass.c |__include __help.h |__makefile makefile: VPATH=include:src vpath %.c src vpath %.h inclue main: testClass.o gcc -o main testClass.o testClass.o: help.h testClass.c . gcc -c testClass.c -o testClass.o

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧