Nim
Nim

Reputation: 33645

Qt and MOC woes with a simple make

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?

Upvotes: 3

Views: 1991

Answers (2)

Daniel V&#233;rit&#233;
Daniel V&#233;rit&#233;

Reputation: 61626

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

Upvotes: 7

MadScientist
MadScientist

Reputation: 100946

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

Upvotes: 1

Related Questions