Qt and MOC woes with a simple make

拥有回忆 提交于 2020-01-13 19:06:09

问题


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

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))

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

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?

My starting point is dumb:

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.

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


回答1:


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 $@

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 $@



回答2:


I don't know anything about QT or moc, but if what you're saying is that moc is a tool that takes a single header file and generates a single .C file, then you can just use a pattern rule:

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


来源:https://stackoverflow.com/questions/17325540/qt-and-moc-woes-with-a-simple-make

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!