4

I usually develop application on OSX and iOS. I found that Objective-C is quite useful for many tasks and, in many cases, can speed up work.

I would like to port my applications also on Windows systems. I realized (if you have any other solutions please let me know!) that Qt could be a quite useful tool! Creating GUI with Qt is simple and fast. The problem is that Qt works with C++ and OSX works with Objective-C.

I read that Qt can handle Objective-C sources, in fact you can find the OBJECTIVE_SOURCES and OBJECTIVE_HEADERS makefile variables.

I searched how to configure Qt Creator to accept .mm files, but I was not able to get it to work.

Inside the .pro file I have something like following:

    ...
    OBJECTIVE_SOURCES += \
        test.mm
    INCLUDEPATH += "C:\GNUStep\...\Headers" 
    LIBS += -L"C:\GNUStep\...\Libraries" -lobjc -lgnustep-base
    QMAKE_CXXFLAGS += -fconstant-string-class=NSConstantString -std=c99
    ...

My very simple project is a widget (.h and .cpp), the main.cpp and test.h with its test.mm. To mix C++ and Objective-C I thought about a "bridge" (just like I do in the Cocoa when mixing Objective-C and C++), so I create the test.h as a simple header (no Objective-C, only a bridge):

    // test.h
    void testFunction( void );

than:

    // test.mm
    #import <Foundation/Foundation.h>

    void testFunction( void ) {
       NSString* string;

       string = @"This is a test string";
       NSLog( @"%@", string ); 
    }

finally:

    // main.cpp
    ...
    #include "test.h"

    ...
    testFunction();
    ...

The linker gives me the classic "undefined reference to function testFunction". Something like it can not link the .o object files created from .mm files.

Can anyone of you tell me if something is wrong with this process or points me to a tutorial on how to configure Qt to accept Objective-C language?

There is another solution that I really like, it's pure Objective-C, i.e. using the GNUStep Gorm interface builder, but, unfortunately, GUIs built with Gorm are completely not accessible with accessibility tools like Jaws or NVDA, and I need to create some applications for visually impaired people!

Thank you very much

Altair Jones
  • 834
  • 1
  • 7
  • 20

1 Answers1

2

The linker error "undefined reference to function testFunction", I suspect is due to a lack of something being defined in the testFunction.

You're right in that to mix Objective-c with Qt you simply create a header with C functions and an implementation file with a .m or .mm extension in which you can write objective-c code and reference the implementation in the .pro under OBJECTIVE_SOURCES

As you're using objective-c and classes such as NSString, you're also going to need to link to the relevant framework(s). In this case, it's Foundation.

So, in your config (.pro) file:-

LIBS += -framework Foundation 

If you want to add another, such as Appkit, it would then be:-

LIBS += -framework Foundation 
LIBS += -framework AppKit

The only other addition to my .pro files when I mix the languages is this:-

QMAKE_CXXFLAGS += -F/Library/Frameworks
QMAKE_CFLAGS += -F/Library/Frameworks
QMAKE_LFLAGS += -F/Library/Frameworks

As you reference NSString in the test function implementation, you'll need to include its definition, so just add it to the includes, as you would normally do in an Objective-C program:-

#import <Foundation/NSString.h>
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Thanks, now I will try this. Really I included the Foundation.h in test.mm, i forgot to mention. – Altair Jones Aug 27 '14 at 08:31
  • Well... I do not have c:\GNUStep\...\Library\Frameworks in my installation folder, and "LIBS += -framework ..." gives the "unrecognized option -framework", maybe I miss something, or I simplified your suggestions... Could you please give me another hint? – Altair Jones Aug 27 '14 at 08:48
  • C:\GNUStep... is a Windows path, not OSX! – TheDarkKnight Aug 27 '14 at 08:50
  • I've corrected the example regarding -framework option. – TheDarkKnight Aug 27 '14 at 08:54
  • @Marlin069 I this question is about mixing Qt and objc on Windows! So the objc compilers, as I know, is GNUStep... – Altair Jones Aug 27 '14 at 08:57
  • Ah, sorry I missed that. Ok, so where are we up to then? You'll need to reference the frameworks that are provided by GNUStep. The "LIBS += framework Foundation" automatically knows where to find Foundation on OS X as it's integral to the OS, so I expect you need to substitute the correct path to the Foundation framework with something like LIBS += -framework \Foundation – TheDarkKnight Aug 27 '14 at 09:20
  • `LIBS += -framework AppKit` throws Linking Error, even without that application compiles – TruthSeeker Dec 31 '20 at 11:42