2011-04-24 35 views
0

我有一个目录,它下面像这样4个子目录一个makefile:如何创建几个子目录

myDir: 
myDir/Part1 
myDir/Part2 
myDir/Part3 
myDir/shared 

我想打一个可执行文件,从共享文件的需要,它链接到文件的第2部分并将可执行文件放入myDir中。

这是我尝试(在makefile是相关仅行):在Makefile

Part2/part2code.o: ../Shared/helper.o 
gcc -ansi -pedantic-errors -c -Wall -Werror -g -o Part2/part2code.o Part2/part2code.c 

,并在它上面:在makefile

Shared/helper.o: 
gcc -ansi -pedantic-errors -c -Wall -Werror -g -o Shared/helper.o Shared/helper.c 

,并在其上方

part2code: Part2/part2code.o ../Shared/helper.o 
gcc -ansi -pedantic-errors -Wall -Werror -g -lm -o part2code Part2/part2code.o ../Shared/helper.o 

(I也尝试不../共享之前)

我得到这个错误:

No such file or directory. 

帮助?

谢谢!

回答

2

在这种情况下,文件名中的路径都与makefile所在的位置相关。所以例如Part2/part2code.o: ../Shared/helper.o不正确;它应该简单地是Part2/part2code.o: Shared/helper.o(依此类推)。还要注意,你已经在你的makefile中写入了Shared,但是你已经将你的目录列为shared ...

尽管实际上,这仍然是错误的。 a: b等规则表示b先决条件a;即在制作b之前,您无法制作a。你的目标文件不是这种情况;他们不相互依赖。通常,目标文件完全取决于其组成源文件(*.c*.h)。因此,举例来说,你的part2code.o规则可能是这样的:(注意其分别替代在目标和先决条件,使用特殊变量[email protected]$^的)

Part2/part2code.o: Part2/part2code.c 
    gcc -ansi -pedantic-errors -c -Wall -Werror -g -o [email protected] $^ 

+0

我相信海报说他没有../就尝试过。 – 2011-04-24 13:35:29

+0

@Konstantin:的确他/她做到了。但有了这些信息,它肯定没有。 – 2011-04-24 14:09:26

相关问题