为什么用qmake生成Visual Studio项目时会忽略我的MOC_DIR [英] Why is my MOC_DIR ignored when generating Visual Studio projects with qmake

查看:254
本文介绍了为什么用qmake生成Visual Studio项目时会忽略我的MOC_DIR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

但是:我仍然收到相同的MSB8027生成警告.这似乎是

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆