棘手的“制作”目标要求
我遇到一种情况,我需要将库中的一些源文件编译到我自己的程序中。我无法写入源文件所在的目录。相反,我有一个本地“构建”目录,所有工作都在其中完成。
我遇到的问题是路径的转换。源文件的名称为 xxxx.cpp
和 yyyy.cpp
,它们位于 /path/to/source/xxxx/xxxx.cpp和<代码>/path/to/source/yyyy/yyyy.cpp。
使用 $(patsubst ...) 我可以愉快地将这些路径转换为 build/xxxx/xxxx.cpp
等,但我无法让它剥离第一个 xxxx
离开。
我可以制作一个与以下内容相匹配的目标:
build/%.o: /path/to/source/%/%.cpp
$(CXX) ...
……但我根本无法让它发挥作用。我想它不喜欢目标后半部分的双通配符。
名称的“来源”是一个变量,只有“xxxx”和“yyyy”:
SYS_LIBS = xxxx yyyy
关于如何让这样的东西工作有什么建议吗?
哦,我需要它成为一个“通用”解决方案 - 这将是许多使用此文件库的项目中包含的 makefile,因此不能选择为每个文件手工制作一个目标。我无法预测库中将包含哪些文件。
I have a situation where I need to compile some source files from a library into my own program. The directories the source files are in are not writeable by me. Instead I have a local "build" directory where all the work is done.
The problem I am having the the translation of the paths. The source files are named, say xxxx.cpp
and yyyy.cpp
, and they are in /path/to/source/xxxx/xxxx.cpp
and /path/to/source/yyyy/yyyy.cpp
.
Using $(patsubst ...) I can happily convert those paths to build/xxxx/xxxx.cpp
etc, but I can't get it to strip the first xxxx
off.
I could do with crafting a target that would match something like this:
build/%.o: /path/to/source/%/%.cpp
$(CXX) ...
...but I can't get that to work at all. I guess it doesn't like the double wildcard in the latter part of the target.
The "source" for the names is a single variable with just the "xxxx" and "yyyy" in:
SYS_LIBS = xxxx yyyy
Any suggestions on how to get something like this to work?
Oh, I need it to be a "generic" solution - this will be an included makefile in many projects that use this library of files, so hand-crafting a target per file is not an option. I cannot predict what files will be in the library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种简单的方法是使用 vpath 让 make 找到文件本身。您只需定义
您可以通过这种方式定义多个源路径,但如果您在多个文件夹中有相同的文件(egacpp 在 path_to_source1 和 path_to_source2 中),请小心
One easy way is to use vpath to let make find the files itself. You just define
You can define more than one path to source that way, but be careful if you have the same file in more than one folder (e.g. a.cpp in both path_to_source1 and path_to_source2)
就我个人而言,我会使用
vpath
,正如 Bruce 建议的那样。如果/path/to/source/
下有很多目录,您可以使用(只要目录仅从那里向下一层即可。如果有类似
/path/ 的内容,则 可以使用到/source/foo/bar/zzzz/zzzz.cpp
,那么你必须依靠find
之类的东西。)如果你真的想进行路径转换,这将这样做:
或者(我不知道你为什么想要这样做,但你可以):
Personally I'd use
vpath
, as Bruce suggests. If there are many directories under/path/to/source/
you can use(This works as long as the directories go only one level down from there. If there are things like
/path/to/source/foo/bar/zzzz/zzzz.cpp
, then you'll have to fall back on something likefind
.)If you really want to do path translation, this will do it:
Or (I don't know why you'd want to do it this way, but you could):