Objective-C in Qt with Mavericks

2019-01-19 04:44发布

问题:

I've been using Objective-C mixed with C++ in Qt without any issues; using .mm files where required.

After upgrading my build machine to Mavericks, I initially noticed that the framework headers were missing, so installed the XCode command line tools, which fixed the issue.

Now, I'm getting a problem compiling Objective-C files with errors complaining about code in the frameworks. For example: -

System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSUserNotification.h:16:44: error: missing ',' between enumerators NSUserNotificationActivationTypeReplied NS_AVAILABLE(10_9, NA) = 3

And

/System/Library/Frameworks/AppKit.framework/Versions/C/Headers/NSApplication.h:58:34: error: expected ';' after top level declarator typedef NSInteger NSModalResponse NS_AVAILABLE_MAC(10_9);

I've upgraded to Qt 5.2.1, but the issues remain and it's coming from including standard framework headers; in this case: -

#import <NSUserNotification.h>
#import <NSApplication.h>

Can someone please explain what's changed in Mavericks and how I can fix these errors?

回答1:

You're supposed to include the frameworks as Framework/Header.h. It seems that you've added some unnecessary includes to your project file.

The following works for me:

#project.pro
TEMPLATE = app

LIBS += -framework AppKit -framework Foundation

OBJECTIVE_SOURCES = main.mm
//main.mm
#import <Foundation/NSUserNotification.h>
#import <AppKit/NSApplication.h>
#include <QCoreApplication>

int main(int argc, char ** argv)
{
   QCoreApplication a(argc, argv);
   NSApplication * app = [NSApplication sharedApplication];
   return 0;
}