Qt和MOC的简单制造就给我们带来了麻烦 [英] Qt and MOC woes with a simple make

查看:91
本文介绍了Qt和MOC的简单制造就给我们带来了麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这比Qt和moc更像是一个GNU Make问题,但这是可行的.

I guess this is more of a GNU Make question than Qt and moc, but here goes.

我有一个包含多个Q_OBJECTS的目录,并且我有一些简单的代码可以收集所有这些信息,例如

I have a directory which contains multiple Q_OBJECTS, and I have some simple code, which collects all these, e.g

MOCS := $(shell grep -l Q_OBJECT $(HEADERS))

假设HEADERS是当前位置"中头文件的集合

Assume HEADERS is a collection of the header files in the "current location"

现在我想做的是指定一条规则,该规则将调用Qt moc来构建.C文件,然后将其编译为一个对象.如何指定这样的规则?

Now what I would like to do is specify a rule which will call Qt moc to build the .C file and then compile that into an object. How would one specify such a rule?

我的出发点是愚蠢的:

MOC_SOURCES := $(MOCS:%.h=%.C)
MOC_OBJECTS := $(MOC_SOURCES:%.C=%.o)

$(MOC_SOURCES) : $(MOCS)
  @echo "Building Moc: $@ from $<"
  $(MOC) $< -o $@

这很愚蠢,因为发生的是$<第一个标头,因此所有对象都是使用此标头而不是相关标头编译的.

This is dumb because, what is happening is that $< the first header, and so all the objects are compiled using this header rather than the related headers.

是否有一种方法可以清理此问题,以便从正确的标头中构建正确的moc对象?

Is there a way to clean this up so that the correct moc object is built from the correct header?

推荐答案

要从每个$(MOC)组件中生成每个$(MOCS_SOURCES)组件,可以使用

To produce each $(MOCS_SOURCES) component from each $(MOC) component, you may use a static pattern rule, like this:

$(MOC_SOURCES) : %.C: %.h
    @echo "Building Moc: $@ from $<"
    $(MOC) $< -o $@

另一种常见的做法是使用隐式规则,但是对于由moc生成的C ++文件具有特定的后缀.这是qmake不能使用的情况:

Another common practice is to use implicit rules, but with a specific suffix for C++ files produced by moc. Here's what I use when qmake is not an option:

%.moc.o: %.moc.cpp
    $(CXXCOMPILE) $(QT_CPPFLAGS) $(QT_CXXFLAGS) -c $< -o $@

%.moc.cpp: %.h
    $(MOC) $< -o $@

这篇关于Qt和MOC的简单制造就给我们带来了麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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