如何用makefile编译多个目录之间的.h .c文件呢

MichaelBomb 2011-09-18 01:36:56
我的linux下,有这几个目录:
~/tmp/test/learn/ 目录下文件:main.c。 里面主函数调用helloworld.c的helloworld函数,以及test.c下的test函数。
main.c代码:

#include <stdio.h>
#include "helloworld.h"
#include "test.h"
int main()
{
test();
helloworld();
return 0;
}

~/tmp/test/learn/math/ 目录下文件: test.h test.c 。里面函数都是简单输出一句话
~/tmp/test/ 目录下文件: helloworld.h helloworld.c。 简单输出一句话

然后我在main.c所在的目录下,即~/tmp/test/learn/ 建立了一个Makefile文件。
像这样主函数调用的函数在不同目录下的头文件中,这样的Makefile该如何写呢?
我试着写了一个 一直提示 test.h 不存在:test.h: No such file or directory.

大虾们,可否写个makefile瞧瞧,应该比较简单吧~,并解释下原因?十分感谢。
...全文
2990 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
delphiwcdj 2011-10-20
  • 打赏
  • 举报
回复
每个目录都写一个makefile,然后在最顶层目录即工程的根目录中的makefile里调用每一个子目录的makefile
fangzhaoguo 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 michaelbomb 的回复:]
我的linux下,有这几个目录:
~/tmp/test/learn/ 目录下文件:main.c。 里面主函数调用helloworld.c的helloworld函数,以及test.c下的test函数。
main.c代码:


C/C++ code

#include <stdio.h>
#include "helloworld.h"
#include "test.h"
int main()
{
……
[/Quote]
因为你的那两个文件没有头文件,都只是资源文件,所以不能使用#include "....h"
应该是这样:

#include <stdio.h>
#include "helloworld.c"
#include "test.c"
int main()
{
test();
helloworld();
return 0;
}



你没有建立helloworld.h和test.h,人家自然找不到这两个文件呀
qq120848369 2011-10-20
  • 打赏
  • 举报
回复
gcc -c main.c helloworld.h -I ../ test.h -I ./math/

头文件不需要参与编译,你只要-I指定它的位置就足够了。。。
gwideal 2011-10-20
  • 打赏
  • 举报
回复
这个问题下载解决了么?我也碰到了类似的问题。
dongjiawei316 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 michaelbomb 的回复:]
我的linux下,有这几个目录:
~/tmp/test/learn/ 目录下文件:main.c。 里面主函数调用helloworld.c的helloworld函数,以及test.c下的test函数。
main.c代码:

C/C++ code


#include <stdio.h>
#include "helloworld.h"
#include "test.h"
int m……
[/Quote]
这样还不够,test.c和helloworld.c还没有编译
宇宙漫步者 2011-09-19
  • 打赏
  • 举报
回复
设置VPATH系统变量或者是Makefile的关键字vpath,设置文件搜索目录
cqbamboo 2011-09-18
  • 打赏
  • 举报
回复
我简单帮你试了一下


sandbox$ tree
.
└── tmp
└── test
├── helloworld.c
├── helloworld.h
└── learn
├── main.c
├── main.o
└── math
├── test.c
└── test.h

4 directories, 6 files
sandbox$ cd tmp/test/learn/
learn$ gcc -c main.c -I ../ -I ./math/
learn$ ls
main.c main.o math

半字节 2011-09-18
  • 打赏
  • 举报
回复
[zaiwei@LenovoE40 ~]$ mkdir test
[zaiwei@LenovoE40 ~]$ cd test
[zaiwei@LenovoE40 test]$ cat >helloworld.c
#include <stdio.h>
void helloworld(void)
{
printf("00000\n");
}
[zaiwei@LenovoE40 test]$ cat >helloworld.h
extern void helloworld(void);
[zaiwei@LenovoE40 test]$ mkdir learn
[zaiwei@LenovoE40 test]$ cd learn
[zaiwei@LenovoE40 learn]$ cat >main.c
#include <stdio.h>
#include "helloworld.h"
#include "test.h"
int main()
{
test();
hello();
return 0;
}
[zaiwei@LenovoE40 learn]$ mkdir math
[zaiwei@LenovoE40 learn]$ cd math
[zaiwei@LenovoE40 math]$ cat >test.c
void test(void)
{
;
}
[zaiwei@LenovoE40 math]$ cat >test.h
extern void test(void)
;
[zaiwei@LenovoE40 math]$ cd ..
[zaiwei@LenovoE40 learn]$ cat makefile
cat: makefile: 没有那个文件或目录
[zaiwei@LenovoE40 learn]$ cat >makefile
BASEDIR=.
.PHONY: all
all: ./main.c ./math/test.c ./../helloworld.c
gcc -o a.out main.c ./math/test.c ./../helloworld.c -I . -I ./.. -I ./math/
#gcc -c -o test.o ./math/test.c -I . -I ./.. -I ./math/
#gcc -c -o helloworld.o ./../helloworld.c -I . -I ./.. -I ./math/
#gcc -o a.out main.o test.o helloworld.o -lc

[zaiwei@LenovoE40 learn]$ make
gcc -o a.out main.c ./math/test.c ./../helloworld.c -I . -I ./.. -I ./math/
#gcc -c -o test.o ./math/test.c -I . -I ./.. -I ./math/
#gcc -c -o helloworld.o ./../helloworld.c -I . -I ./.. -I ./math/
#gcc -o a.out main.o test.o helloworld.o -lc
[zaiwei@LenovoE40 learn]$ ls
a.out helloworld.o main.c main.o makefile math test.o


可折腾死我了,上面写main.c的时候出了点问题,函数名子应该是helloworld()
弄得我半天编译不过。。。
MichaelBomb 2011-09-18
  • 打赏
  • 举报
回复
我这样了还是不行啊:
gcc -c main.c helloworld.h -I ../ test.h -I ./math/

另:可否写个Makefile , 我主要是要学习怎么写这种情况的makefile。
[Quote=引用 1 楼 wangzaiwei2006 的回复:]
test.h不存在是因为你没有给指定test.h的路径,因为没有指定的话头文件搜索会以这个顺序
1,当前目录
2,系统目录
所以 你现在有两个办法
1,在包含头文件的时候 以当前目录开始
也就是说你需要把这里#include "test.h" 改成#include "./math/test.h"
2,在makefile中用 -I来指定搜索目录
比如makefile中这样写 gcc -……
[/Quote]
半字节 2011-09-18
  • 打赏
  • 举报
回复
test.h不存在是因为你没有给指定test.h的路径,因为没有指定的话头文件搜索会以这个顺序
1,当前目录
2,系统目录
所以 你现在有两个办法
1,在包含头文件的时候 以当前目录开始
也就是说你需要把这里#include "test.h" 改成#include "./math/test.h"
2,在makefile中用 -I来指定搜索目录
比如makefile中这样写 gcc -I ./math ......其他乱七八糟参数

23,121

社区成员

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

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