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. Make *.mm files compilable on all platforms

Make *.mm files compilable on all platforms

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 4.0k Views
  • 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.
  • M Offline
    M Offline
    m_andrej
    wrote on last edited by m_andrej
    #1

    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.

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

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        m_andrej
        wrote on last edited by
        #3

        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
        }
        
        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          ah super.

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

            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 ?

            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
            1
            • M Offline
              M Offline
              m_andrej
              wrote on last edited by
              #6

              Because

              • I don't know how to do it
              • I don't see how it's better from my current solution ;-)
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7
                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 ?

                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
                1
                • M Offline
                  M Offline
                  m_andrej
                  wrote on last edited by
                  #8

                  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.

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

                    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.

                    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

                    • Login

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