Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Make *.mm files compilable on all platforms

    General and Desktop
    3
    9
    2192
    Loading More Posts
    • 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
      m_andrej last edited by m_andrej

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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 Reply Quote 0
        • M
          m_andrej last edited by

          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 Reply Quote 1
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            ah super.

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 1
              • M
                m_andrej last edited by

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 1
                  • M
                    m_andrej last edited by

                    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 Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 0
                      • First post
                        Last post