Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The right way to use Objective C++ and Qt



  • Hi everybody. I have to implement NSString in my program.
    In order to use NSString I have created a header file and a mm file. See below:

    @#ifndef A_H
    #define A_H

    #import <Foundation/NSString.h>

    void CanTry();

    #endif A_H@

    The mm file

    @#include "a.h"

    void CanTry() {
    }@

    the .pro file

    @OBJECTIVE_HEADERS += a.h
    OBJECTIVE_SOURCES += a.mm

    LIBS += -framework Foundation@

    but I still obtain the error:

    unknow type name NSString NSObjRuntime.h
    unknow type name Protocol NSObjRuntime.h
    ...

    What I have missed?


  • Lifetime Qt Champion

    Hi,

    The idea is basically to avoid having any Objective-C element in the header you want to use with pure C++ code. You create an interface and only use Objective-C code in the implementation.

    Hope it helps



  • As SGaist said;

    In your program (C/C++) you need to include a.h so the program knows about CanTry() but you can't simply include an Objective C header into C/C++.

    To make your simple example work, make a.h a simple C header by moving the #import to the .mm

    This is nothing to do with Qt.


Log in to reply