为什么用qmake生成Visual Studio项目时会忽略我的MOC_DIR [英] Why is my MOC_DIR ignored when generating Visual Studio projects with qmake
问题描述
我正在使用Qt 5.3 qmake
生成Visual Studio 2013项目.在我的.pro
文件中,有以下一行:
I'm generating Visual Studio 2013 projects with Qt 5.3 qmake
. In my .pro
file, I've got the following line:
MOC_DIR = $$BUILD_DIR/<DEBUG OR RELEASE>/moc
如果我message($$MOC_DIR)
,则路径正确形成.但是,当我在VS中构建文件时,不会在该位置生成moc_<CLASS>.cpp
文件,而是将其与.pro
放在同一目录中.在编译过程中,我收到以下警告:
If I message($$MOC_DIR)
, the path is correctly formed. However, when I build in VS, the moc_<CLASS>.cpp
files are not generated in that location, but instead end up in the same directory as the .pro
. I get the following warning during compilation:
两个或多个名称为
moc_<CLASS>.cpp
的文件将产生输出到同一位置
Two or more files with the name of
moc_<CLASS>.cpp
will produce outputs to the same location
这不足为奇,因为如果我查看生成的.vcxproj
的内容,则会看到以下内容(不相关的标签/文本已删除...
):
That's not surprising, because if I look at the contents of the generated .vcxproj
, I see the following (irrelevant tags/text elided ...
):
<CustomBuild Include="..\include\Class.hpp">
...
<Outputs Condition="...Release...">moc_Class.cpp;%(Outputs)</Outputs>
...
<Outputs Condition="...Debug...">moc_Class.cpp;%(Outputs)</Outputs>
...
</CustomBuild>
为什么我的自定义MOC_DIR
被忽略?
Why does is my custom MOC_DIR
being ignored?
推荐答案
使用Qt5 qmake
生成的其他VS2013项目正在将moc_<CLASS>.cpp
文件放置在指定位置,正如预期的那样,所以我怀疑是在.pro
正在干扰.
Other VS2013 projects generated with Qt5 qmake
were placing the moc_<CLASS>.cpp
files in the specified location, as expected, so my suspicion was that something else in the .pro
was interfering.
我正在使用Boost(1.55),并将Boost添加到项目中时,遇到了此SO问题中描述的问题:
I'm using Boost (1.55) and while adding Boost to the project, I came across the issue described in this SO question: link.
我在.pro
的顶部添加了以下几行:
I added the following lines to the top of my .pro
:
load(moc)
QMAKE_MOC += -DBOOST_MPL_IF_HPP_INCLUDED
...,这解决了我的宏参数不匹配错误.这也导致了此处描述的错误.出于某些原因在设置MOC_DIR
之前调用load(moc)
会导致qmake
忽略自定义MOC_DIR
.如果您将load(moc)
重新排序为设置了MOC_DIR
之后的时间,则一切都会按预期进行.
... and that sorted out my macro argument mismatch bug. It also caused the bug described here. Calling load(moc)
before setting MOC_DIR
for some reason causes qmake
to ignore the custom MOC_DIR
. If you reorder load(moc)
to be after MOC_DIR
is set, everything works as expected.
我的.vcxproj
现在看起来应该是应该的,我的moc_<CLASS>.cpp
文件现在放置在正确的<BUILD>/debug/moc
或<BUILD>/release/moc
目录中.
My .vcxproj
now looks as it should, my moc_<CLASS>.cpp
files now placed into the correct <BUILD>/debug/moc
or <BUILD>/release/moc
directories.
However: I still get the same MSB8027
build warnings. This appears to be a known bug in Visual Studio. It confuses the situation, though, as I only broke the moc locations when I added load(moc)
, but I got the warning before the problem was introduced, while it existed, and now even after it's fixed! For now I'm disabling the warning; see Felix Huang's answer here.
这篇关于为什么用qmake生成Visual Studio项目时会忽略我的MOC_DIR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!