MessageBox with MinWG



  • I created a subclass MessageBox of QMessageBox that has some static functions, including one named error. Using these functions elsewhere gives a compilation error with MinWG but not with gcc. The error looks a bit odd:

    error: undefined reference to `MessageBoxW::error(QString const&, QString const&, QFlags<QMessageBox::StandardButton>, QWidget*)'
    

    The oddity for me is in the letter W after MessageBox in the reference. When diving into this, I found that there is a global #define in winuser.h as follows:

    #define MessageBox __MINGW_NAME_AW(MessageBox)
    

    which already claims the MessageBox symbol. Replacing the above code in winuser.h into for example

    #define MINWGMessageBox __MINGW_NAME_AW(MessageBox)
    

    solves my compilation problem and the application runs correctly.

    Has anyone encountered this problem before? Is it save to fix the problem in winuser.h or should I better rename my class (not really my preference)?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    No it's not safe to fiddle with winuser.h. See here for more information about the MessageBox macro.

    Short version: MessageBox is a Windows API wrapped in a macro by MinGW to get the correct function.



  • That's what I guessed as well...

    Now, I got the opportunity to use the msvc2012 compiler for the same code which also gives a compiler error, with a similar oddity

    mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: static int __cdecl MessageBoxW::error(class QString const &,class QString const &,class QFlags<enum QMessageBox::StandardButton>,class QWidget *)" (?error@MessageBoxW@@SAHABVQString@@0V?$QFlags@W4StandardButton@QMessageBox@@@@PAVQWidget@@@Z) referenced in function "private: void __thiscall MainWindow::openFile(void)" (?openFile@MainWindow@@AAEXXZ)
    

    Is renaming my MessageBox class the only way out here?



  • Is renaming my MessageBox class the only way out here?

    no, this is exactly the reason namespaces were invented



  • Yes, of course! Didn't think about that. I am still too much of a C++ novice...

    Solved :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.