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.
-
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 }
-
ah super.
-
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 ?
-
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 ?
-
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.