3

I have a Qt4.8.4 desktop application that builds and runs fine on my Macbook Pro, running Mountain Lion with Xcode 5.0.2, using Qt Creator 2.7.0 with Qt 4.8.4 and GCC (x86 64bit). I am trying to port my application to Qt 5.2.1. My code is C++ with some Objective-C.

I built Qt5.2.1 on my same Macbook pro with this configuration:

./configure -prefix $PWD/qtbase -debug-and-release -developer-build -no-c++11 -opensource -plugin-sql-sqlite -nomake tests -confirm-license

and it configured and built fine.

When I try to build my application in Qt Creator 2.7.0 using Qt 5.2.1 and either GCC (x86 64bit) or Clang (x86 64bit), I get lots of errors that seem to me to indicate that the Objective-C parts of my application can't find the libraries they need. For example:

/Users/david/dev/svn/map_creator3/src/widgets/mac_toolbar_button_proxy.mm:15: warning: instance method '-selectedSegment' not found (return type defaults to 'id') [-Wobjc-method-access]
    target_->TriggerAction([sender selectedSegment]);
                                   ^~~~~~~~~~~~~~~
/Users/david/dev/svn/map_creator3/src/common/locations_mac.mm:34: error: use of undeclared identifier 'NSWorkspace'
      [[NSWorkspace sharedWorkspace]
        ^
/Users/david/dev/svn/map_creator3/src/widgets/mac_toolbar_button_control.mm:30: error: unknown type name 'NSAutoreleasePool'
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  ^

and lots more like that. Is there some magic setting somewhere that I need to add, to use Object-C in a C++ Qt5 desktop application?

David Burson
  • 2,947
  • 7
  • 32
  • 55
  • Have you included the relevant frameworks in the .pro file? – TheDarkKnight Mar 14 '14 at 11:17
  • what frameworks do I need to include? I tried adding QT += macextras, that had no effect. – David Burson Mar 14 '14 at 16:54
  • 1
    Whatever you're using. For example, I have this in my .pro: QMAKE_LFLAGS += -F /System/Library/Frameworks/Foundation.framework/ LIBS += -framework Foundation – TheDarkKnight Mar 14 '14 at 17:23
  • 1
    And any .mm files are added like this: OBJECTIVE_SOURCES += ObjCHelper.mm – TheDarkKnight Mar 14 '14 at 17:24
  • I have this: macx:LIBS += -framework Foundation -framework Security -framework AppKit. I tried adding the associated QMAKE_LFLAGS, that didn't make a difference. I do have my .mm's added as you mention, with OBJECTIVE_SOURCES. Any other thoughts? – David Burson Mar 14 '14 at 18:58
  • You could try including the headers too: INCLUDEPATH += /System/Library/Frameworks/AppKit.framework/Versions/C/Headers – TheDarkKnight Mar 17 '14 at 10:37
  • Having upgraded to Mavericks, I've since discovered that adding the frameworks in the .pro is wrong. You need to add the lib and then include the library header you want. So that's "LIBS += -framework Foundation" and to include a header #import – TheDarkKnight Mar 17 '14 at 17:10
  • It seems that Mountain Lion isn't as fussy to let you do the wrong thing, but this should be all you need to do, once you've added the header and OBJECTIVE_SOURCES – TheDarkKnight Mar 17 '14 at 17:11
  • adding headers to INCLUDEPATH didn't help either. Thanks for all the thoughts on this. I'm out a couple days, then I may create a minimal example. – David Burson Mar 17 '14 at 22:10

1 Answers1

4

Merlin069 got me on the right track in the comments on my question (thanks!), and I found an example here. These build errors (and a number of others) disappeared when I added

LIBS += -framework Cocoa

and in my .mm files that were complaining,

#include <Cocoa/Cocoa.h>
Community
  • 1
  • 1
David Burson
  • 2,947
  • 7
  • 32
  • 55