Search code examples
xcodeqtqobjectmoc

How to compile the header file with Q_OBJECT macro in Xcode?


I create C++ project in Xcode which links against the Qt framework. The hello world program works well. When I add a class derived from QObject and add the Q_OBJCET macro, there is link error.

The class is

MyObject.h

#ifndef MyObject_h
#define MyObject_h

#include <QtCore/QObject>

class MyOBject : public QObject
{
  Q_OBJECT

public:
    MyOBject();

};


#endif

MyObject.cpp

#include "MyObject.h"

MyOBject::MyOBject()
{
}

I know I should use the moc to compile the MyObject.h first and add the generated moc_MyObject.cpp to the Xcode project.

In Microsoft Visual Studio, I can configure this header file to be compiled with moc custom tool. And add the generate cpp file to VS project.

But this is Xcode. My question is: Is there equivalent mean in Xcode to compile the header file including Q_OBJECT macro?


Solution

  • I didn't find how to set the custom tool for a specific header file in Xcode. I found a workaround via the build phase script (Build Phases -> Add Build Phase -> Add Run Script). I added the moc command line as script, and included the generated moc_MyObject.cpp file to MyObject.cpp. It works now.