棘手的“制作”目标要求

发布于 2024-12-02 04:12:16 字数 730 浏览 1 评论 0原文

我遇到一种情况,我需要将库中的一些源文件编译到我自己的程序中。我无法写入源文件所在的目录。相反,我有一个本地“构建”目录,所有工作都在其中完成。

我遇到的问题是路径的转换。源文件的名称为 xxxx.cppyyyy.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-12-09 04:12:16

一种简单的方法是使用 vpath 让 make 找到文件本身。您只需定义

vpath %.cpp /path/to/source
vpath %.cpp /path/to/source2
vpath %.cpp /path/to/source3

build/%.o : %.cpp
   $(CXX)   

您可以通过这种方式定义多个源路径,但如果您在多个文件夹中有相同的文件(egacpp 在 path_to_source1 和 path_to_source2 中),请小心

One easy way is to use vpath to let make find the files itself. You just define

vpath %.cpp /path/to/source
vpath %.cpp /path/to/source2
vpath %.cpp /path/to/source3

build/%.o : %.cpp
   $(CXX)   

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)

奶茶白久 2024-12-09 04:12:16

就我个人而言,我会使用 vpath,正如 Bruce 建议的那样。如果 /path/to/source/ 下有很多目录,您可以使用

SRCS = $(wildcard /path/to/source/*)
vpath %.cpp $(SRCS)

(只要目录仅从那里向下一层即可。如果有类似 /path/ 的内容,则 可以使用到/source/foo/bar/zzzz/zzzz.cpp,那么你必须依靠find之类的东西。)

如果你真的想进行路径转换,这将这样做:

X := $(patsubst %.cpp,build/%.o,$(notdir $(X)))

或者(我不知道你为什么想要这样做,但你可以):

X := $(shell echo $(X) | sed 's|.*/\(.*\)\.cpp|build/\1\.o|')

Personally I'd use vpath, as Bruce suggests. If there are many directories under /path/to/source/ you can use

SRCS = $(wildcard /path/to/source/*)
vpath %.cpp $(SRCS)

(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 like find.)

If you really want to do path translation, this will do it:

X := $(patsubst %.cpp,build/%.o,$(notdir $(X)))

Or (I don't know why you'd want to do it this way, but you could):

X := $(shell echo $(X) | sed 's|.*/\(.*\)\.cpp|build/\1\.o|')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文