制作#include 正方形
我正在尝试在 Solaris 10 上使用 CC 编写 makefile。[我认为只有第一部分真正重要]。我对 foo.o 有以下规则:
foo.o: foo.cc common_dependencies.h
CC -c foo.cc -I../../common
不幸的是,common_dependency.h 在未命名为“.”的目录中包含各种特殊垃圾。或 '../../common' 。这是否必须是一个强力 makefile,我可以在其中找出所有依赖路径?所有依赖项都位于“../..”下的某个位置,但有时向下 1 级,有时向下 2 级。
-谢谢尼尔
I'm trying to write a makefile using CC on Solaris 10. [Only the first bit of that really matters, I think]. I have the following rule for foo.o:
foo.o: foo.cc common_dependencies.h
CC -c foo.cc -I../../common
Unfortunately, common_dependencies.h includes all sorts of idiosyncratic trash, in directories not named '.' or '../../common' . Is this just going to have to be a brute force makefile where I ferret out all of the dependency paths? All of the dependencies are somewhere under '../..', but sometimes 1-level down and sometimes 2-levels down.
-Thanks Neil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,如果头文件在多个目录中的源文件之间共享,它们将保存在项目根目录下的
include/
树中,然后使用类似-I$(TOPDIR)/ 的内容include
将包含树添加到包含路径,并让源文件引入include/foo/bar.h
,如下所示:Typically, if header files are shared amongst source files in multiple directories, they are kept in an
include/
tree under the project root, and then you use something like-I$(TOPDIR)/include
to add the include tree to the include path and let your source files pull ininclude/foo/bar.h
like so:这个问题总体上来说是相当棘手的。有很多用于项目组织的约定以及可以提供帮助的工具(
autoconf
和类似的),但是如果作者没有利用这些东西,您将不得不搜索所有内容并安装-我是你自己。
您可以使用 gcc -M 或 makedepends 或许多类似工具之一来获取所需目录的列表。想必您甚至可以编写提取脚本。
This problem is pretty mean in general. There are plenty of conventions for project organization plus tools to help (
autoconf
and similar), but if the author hasn't taken advantage of those things you're going to have to hunt everything down and install the-I
s yourself.You could use
gcc -M
ormakedepends
or one of the many similar tools to get a list of the directories that are needed. Presumably you could even script the extraction.在 #include 指令中指定路径确实不是一个好主意;它使标头以及依赖于它的任何内容都依赖于特定的目录结构。值得了解是否允许您清理 common_dependency.h 本身。
如果 common_dependency.h 中的路径正确,那么您的 Makefile 将按原样工作(尽管我建议使用绝对路径,而不是“../..”),并且您可以使用类似 高级自动依赖生成 为您处理依赖项。
如果 common_dependency.h 中的路径不正确,那么是的,您必须找到它 #include 的每个文件,并且知道它们在哪里还不够。如果它 #includes 具有错误路径的文件,即使编译器具有 -Ithe_ Correct_path,编译也会崩溃,因此您必须修复 common_dependency.h 或修改目录结构(通过移动、复制或链接到文件) 。
It's really not a good idea to specify a path in an #include directive; it makes the header -- and anything that depends on it -- dependent on a particular directory structure. It's worth finding out whether you're allowed to clean up common_dependencies.h itself.
If the paths in common_dependencies.h are correct, then your Makefile will work as is (although I'd recommend using an absolute path, not "../.."), and you can use somthing like Advanced Auto-Dependency Generation to handle the dependencies for you.
If the paths in common_dependencies.h are not correct, then yes, you'll have to find every file that it #includes, and knowing where they are won't be enough. If it #includes a file with a false path, the compilation will crash even if the compiler has -Ithe_correct_path, so you'll have to either fix common_dependencies.h or modify the directory structure (by moving, copying or linking to the files).