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

Make *.mm files compilable on all platforms



  • Hello,

    I have a Qt application for iOS that uses some Objective-C snippets in an Objective-C++ file (*.mm extension). Now I need to make the code compile on other platforms. I have wrapped all Objective-C lines in #ifdef Q_OS_IOS blocks, so I need to convince qmake to compile *.mm files as C++. Any idea how to do that?

    I tried the following in my *.pro file:

    HEADERS += \
        ...
        src/CServerCommunicator.h 
    
    ios {
        OBJECTIVE_SOURCES += src/CServerCommunicator.mm
    }
    
    !ios {
        SOURCES += src/CServerCommunicator.mm
    }
    

    It does seem to compile the *.mm file as C++, but only as C++98. It's showing compile errors for things like nullptr.


  • Lifetime Qt Champion

    Hi
    on windows, I have to put
    CONFIG +=C++11
    in my pro file when using mingw to get new c++ so to speak.



  • I already have the line CONFIG += C++11.

    But I managed to solve this by adding QMAKE_EXT_CPP += .mm. At least on Windows this compiles fine:

    HEADERS += \
        ...
        src/CServerCommunicator.h 
    
    ios {
        OBJECTIVE_SOURCES += src/CServerCommunicator.mm
    }
    
    !ios {
        QMAKE_EXT_CPP += .mm
        SOURCES += src/CServerCommunicator.mm
    }

  • Lifetime Qt Champion

    ah super.


  • Lifetime Qt Champion

    Hi,

    Out of curiosity, why not keep the iOS specific code in the *mm file and implement the standard c++ part in a cpp file ?



  • Because

    • I don't know how to do it
    • I don't see how it's better from my current solution ;-)

  • Lifetime Qt Champion

    HEADERS += \
        ...
        src/CServerCommunicator.h 
    
    ios {
        OBJECTIVE_SOURCES += src/CServerCommunicator.mm
    } else {
        SOURCES += src/CServerCommunicator.cpp
    }
    

    It's better in the sense that you clearly separate building code for platform that support Objective-C++ from the others. It also makes your code easier to read and maintain for you and your team mates.

    Just as an example, would you go read an Objective-C++ file if you build a library on Windows ?



  • It seems an interesting idea. But most of my *.mm file contains C++ code which is used also on iOS. Having two files (mm and cpp) would mean having duplicate C++ code in both.


  • Lifetime Qt Champion

    Nothing forbids you to have a common file for code that is the same and two specialized files, one for iOS and one for the other platforms.


Log in to reply