4

I am currently trying to compile a QT based project on IOS. I am using cmake to create and configure the .xcodeproject and xcode to run the app on the device.

I succeed to remove all the previous linker error and now i am dealing with the entry point.

My main.cpp looks like that

    int main(int argc, char **argv) {
        QApplication app(argc, argv);
        return app.exec();
     }

this got me the following error:

Error: You are creating QApplication before calling UIApplicationMain. If you are writing a native iOS application, and only want to use Qt for parts of the application, a good place to create QApplication is from within 'applicationDidFinishLaunching' inside your UIApplication delegate.

I found on this post that you should rename the main and qt will do the job for you and launch the application life cycle

Run-time error for Qt application on ios built via CMake

Qt XCode iOS entry point

#if defined(Q_OS_IOS)
extern "C" int qtmn(int argc, char** argv) {
#else
int main(int argc, char **argv) {
#endif
    QApplication app(argc, argv);
    return app.exec();
}

but now i am dealing with this error

ld: entry point (_main) undefined. for architecture arm64

In the first post they say that i should have a your/qt/root/path/mkspecs/macx-ios-clang/rename_main.sh script

I got none, in the other post the answer says to:

renaming main -> qtmn (in qt sourced), rebuilt QT, and called qt_main_wrapper from my main().

but I don't know what sould i do with the "rebuilt and called qt_main_wrapper"

Paltoquet
  • 1,184
  • 1
  • 10
  • 18
  • 1
    I have never heard of replacing your main() for iOS to work. I have an app released on iTunes Store that certainly works just fine without any special handling (i.e. no #ifdefs required at the main() for iOS, android and win32). My main looks like this: int main(...) { return my_singleton::run(args) }; And my my_singleton::run() is static function which looks like this: int my_singleton::run() { singleton_ = new myQApplication(args) } with myQApplication being a direct descendant of QApplication. – markus-nm Aug 07 '17 at 12:33
  • @markus-nm Thanks, I update my problem here https://forum.qt.io/topic/82102/can-t-create-qapplication-for-ios-using-cmake-build, it looks like Qt is not wrapping my QApplication in a ObjectiveC equivalent – Paltoquet Aug 07 '17 at 13:12

3 Answers3

4

The fix was to add a linker flags

set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Wl,-e,_qt_main_wrapper")

Qt need a UIApplication to be instanciate, it's the application defined inside UIKit. You can file the definition of qt_main_wrapper inside: qtbase/src/plugins/platforms/ios/qioseventdispatcher.mm

The -e option specify an other entryPoint for your program, this will create this UIApplication.

If you follow the constructor of QApplication it will end in the ios plugin and will launch an error if the "UIApplication" singleton is not set.

Finally if your application is using some QML

set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-u _qt_registerPlatformPlugin")

You will need to load all the different plugin inside your main.cpp

Q_IMPORT_PLUGIN(QtQuick2Plugin)

and also add the path to the .a in your LD Flags something like:

set (LINK_LIBRARIES ${LINK_LIBRARIES} "${USE_QT_DIR}/qml/QtQuick/Controls.2/libqtquickcontrols2plugin_debug.a")

for QtControls2 you will also need to add

set (LINK_LIBRARIES ${LINK_LIBRARIES} "${USE_QT_DIR}/lib/libQt5QuickControls2_debug.a")
set (LINK_LIBRARIES ${LINK_LIBRARIES} "${USE_QT_DIR}/lib/libQt5QuickTemplates2_debug.a")

It's a bit cumbersome, but you can run the qmlimportscanner located in Qt/5.7/ios/bin on your project:

qmlimportscanner -rootPath path/to/app/qml/directory -importPath path/to/qt/qml/directory

It will give you a JSON with all the plugin used by your project. It's the script called by qmake, if i have some time i will add a script to add the diferent plugin later.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Paltoquet
  • 1,184
  • 1
  • 10
  • 18
0

I just tested with Qt 5.15.1, and most of the CMake iOS build problems now appear to have been fixed. The only ad-hoc project setting left is

set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Wl,-e,_qt_main_wrapper")

This will also be fixed when https://bugreports.qt.io/browse/QTBUG-87060 is released as part of Qt 6.

0
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Wl,-e,_qt_main_wrapper")

Is working with Qt6.5

If your using qmake and get this error:

Error: You are creating QApplication before calling UIApplicationMain.

Add QMAKE_LFLAGS += -Wl,-e,_qt_main_wrapper to your .pro file works with qt6.5 qt creator 10

zeroalpha
  • 173
  • 1
  • 11