Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The right way to use Objective C++ and Qt
Qt 6.11 is out! See what's new in the release blog

The right way to use Objective C++ and Qt

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrdebugM Offline
    mrdebugM Offline
    mrdebug
    wrote on last edited by
    #1

    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?

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kegon
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved