使用多个 .pri 文件的 Qt MOC 文件名冲突
为了使我的 Qt 项目保持一定的组织性(使用 Qt Creator),我有一个 .pro 文件和多个 .pri 文件。最近,我向其中一个 .pri 文件添加了一个类,该类与单独的 .pri 文件中已存在的类具有相同的文件名。
qmake 生成的文件结构和 makefile 似乎没有注意到随之而来的文件名冲突。生成的 moc_* 文件全部被扔到同一个子目录中(发布或调试,具体取决于),并且一个最终会覆盖另一个。当我尝试制作该项目时,我收到几个如下所示的警告:
Makefile.Release:318: warning: overriding commands for target `release/moc_file.cpp`
并且该项目无法链接。
这是我正在谈论的一个简单例子。
目录结构:
+ project_dir | + subdir1 | | - file.h | | - file.cpp | + subdir2 | | - file.h | | - file.cpp | - main.cpp | - project.pro | - subdir1.pri | - subdir2.pri
project.pro 的内容:
TARGET = project
TEMPLATE = app
include(subdir1.pri)
include(subdir2.pri)
SOURCES += main.cpp
subdir1.pri 的内容:
HEADERS += subdir1/file.h
SOURCES += subdir1/file.cpp
subdir2.pri 的内容:
HEADERS += subdir2/file.h
SOURCES += subdir2/file.cpp
有没有办法告诉 qmake 生成一个系统,将单独的 .pri 文件中的 moc_* 文件放入单独的子目录中?
In order to keep my Qt project somewhat organized (using Qt Creator), I've got one .pro file and multiple .pri files. Just recently I added a class to one of my .pri files that has the same filename as a class that already existed in a separate .pri file.
The file structure and makefiles generated by qmake appear to be oblivious to the filename collision that ensues. The generated moc_* files all get thrown into the same subdirectory (either release or debug, depending) and one ends up overwriting the other. When I try to make the project, I get several warnings that look like this:
Makefile.Release:318: warning: overriding commands for target `release/moc_file.cpp`
And the project fails to link.
Here is a simple example of what I'm talking about.
Directory structure:
+ project_dir | + subdir1 | | - file.h | | - file.cpp | + subdir2 | | - file.h | | - file.cpp | - main.cpp | - project.pro | - subdir1.pri | - subdir2.pri
Contents of project.pro:
TARGET = project
TEMPLATE = app
include(subdir1.pri)
include(subdir2.pri)
SOURCES += main.cpp
Contents of subdir1.pri:
HEADERS += subdir1/file.h
SOURCES += subdir1/file.cpp
Contents of subdir2.pri:
HEADERS += subdir2/file.h
SOURCES += subdir2/file.cpp
Is there a way to tell qmake to generate a system that puts the moc_* files from separate .pri files into separate subdirectories?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 subdir1.pri 中尝试附加
Also for subdir2.pri 给
它没有测试。看看吧。希望它能起作用。
编辑 1:其中 MOCFiles 是您想要存放 moc 文件的文件夹。
编辑 2:我刚刚停止提及 MOC 文件目录,因为问题中已专门询问了这一点。但此外,您可能还需要将以下内容添加到每个 pri 文件中。 (确保不同 *.pri 文件的文件夹不同)
我相信多个 pri 文件可以通过具有相同的文件名而不会发生冲突。既然您已经接受了答案(表明这是不可能的),请进行上述更改并尝试一下。如果它不起作用,请告诉我们。
In subdir1.pri try appending
Also for subdir2.pri give
It isn't tested. Just check it out. Hope it will work.
Edit 1 : Where MOCFiles is your desired folder for your moc files to get into.
Edit 2 : I just stopped mentioning with the MOC files directory since that has been asked specifically in the question. But additionally you may also have to add the following to each of the pri files. (Make sure that the folders are different for different *.pri files)
I believe having multiple pri files can work without collisions by having the same file names. Since you have accepted an answer (which states it is not possible), make the above changes and give a try. Do let know if it isn't working.
最好的办法是确保所有文件都有唯一的名称。除了 qmake 之外,还有其他工具,当你尝试做你正在做的事情时,它们也会崩溃;您也可能会让自己感到困惑(例如,理解
#include "file.h"
的作用更加困难)。Best thing to do is to make sure that all files have a unique name. There are other tools besides qmake which will also break when you try to do what you're doing; you also potentially make it confusing for yourself (e.g. understanding what
#include "file.h"
does is more difficult).我以前尝试过这个。简短的答案是以某种方式以不同的方式命名它们。另一个答案是将每个子目录视为一个单独的库,具有自己的 .pro 文件,并使用 subdirs 类型来编译所有库目录。
如果您确实想研究完整的答案,您可以指定用于 moc 的工具。在这种情况下,您也许可以修改名称,以便为两个不同的文件使用稍微不同的名称。但是,您还需要确保每个不同名称的文件都添加到要编译和链接的文件列表中,而最初命名的 moc 文件则不然(否则您的构建将失败)。
I have tried this before. The short answer is to name them differently somehow. Another answer would be to treat each subdirectory as a separate library, with its own .pro file, and use a subdirs type to compile all the library directories.
If you really want to investigate a full answer, you can specify the tool to be used for moc. In this situation, you might be able to mangle the name so that a slightly different name is used for the two different files. However, you would then also need to make sure each differently-named file is added to the list of files to compile and link, and the originally-named moc file is not (or your build will fail).