QT namespace does not wrap all QT definitions?!



  • I am trying to use QT with another 3rd party sdk as well as my own source code. I have set QT to be wrapped in a namespace to try and avoid any namespace clashes however I am still running into problems with qglobal.h

    In the qglobal.h file there is the following code...

    QT_BEGIN_INCLUDE_NAMESPACE
    typedef unsigned char uchar;
    typedef unsigned short ushort;
    typedef unsigned int uint;
    typedef unsigned long ulong;
    QT_END_INCLUDE_NAMESPACE

    The macros QT_BEGIN_INCLUDE_NAMESPACE and END are being defined as } and { respectively.
    This is removing the typedef code from the QT_BEGIN_NAMESPACE macro and making them global...this in turn then gives a redefinition error as I have already defined ulong differently for my own purposes.

    Have i made a mistake while setting QT up and is there a way to get the QT_***_INCLUDE_NAMESPACE macros to be more sensible without manually editing the source code of QT?

    I am aware that namespacing all of my own functions would fix the issue but I also don't think it should be possible for QT to define things outside of its namespace when using the namespace options.

    Thanks in advance,
    Adam


  • Qt Champions 2016

    Hi and welcome
    Maybe you could add which compiler and platform and Qt version.

    In windows 7, (mingw) compiler, if I make a new project and paste

    typedef unsigned char uchar;
    typedef unsigned short ushort;
    typedef unsigned int uint;
    typedef unsigned long ulong;
    

    in mainwindows.h , no redefinition errors comes.
    So I wonder if you can try the same and see if you get it as
    then I think it's might be a setup issue.



  • Hi mrjj thanks for helping,
    I am using Windows 7 and have tested on QT 5.5 and 4.8.4 both give me the same errors whether compiled with QT creator or with the visual studio plugin.

    Following your example with a blank project I have done the same and can confirm that

    typedef unsigned char uchar;
    typedef unsigned short ushort;
    typedef unsigned int uint;
    typedef unsigned long ulong;

    do not give redefinition errors however the following does give redefinition errors

    typedef unsigned short uchar;
    typedef unsigned int ushort;
    typedef unsigned long uint;
    typedef unsigned char ulong;

    This is because the first typedefs are just redefining them to be the same as they already were and so compilers will either just throw a warning or ignore them altogether whereas when you redefine them to use different data types than the original definition then the compiler cannot reconcile the differences and throws an error.

    Is this the case on your setup as well?

    Thanks,
    Adam


  • Qt Champions 2016

    Hi
    Yes that fails for me too if used in a file that include say
    #include <QApplication>

    So it seems that of all file is automatically in Qt name space.

    Sorry I have not seen any flags to alter this.
    I have just wrapped mine in a namespace.

    Maybe others have good idea.


  • Qt Champions 2016

    @admap
    Hello,
    What binary are you using, the precompiled one? If you want Qt to wrap its classes (respectively typedefs) in a namespace you may need to build it on your own. Here is the list of configure options for Qt 4.8 you can use (note the -qtnamespace switch). By default Qt will not use namespaces.

    Kind regards.



  • Hi @kshegunov

    I have configured QT using the -qtnamespace flag but if you look in the source for qglobal.h the

    QT_BEGIN_NAMESPACE macro works properly to namespace the QT definitions but then in one of those sections there is a QT_BEGIN_INCLUDE_NAMESPACE macro which removes the code from the namespace.

    So there is this situation in the QT qglobal.h file

    QT_BEGIN_NAMESPACE // NAMESPACE_NAME {
    ...
    // namespaced code
    ...
    QT_BEGIN_INCLUDE_NAMESPACE // }
    ...
    // exposed typedefs
    ...
    QT_END_INCLUDE_NAMESPACE // NAMESPACE_NAME {
    ...
    // namespaced code
    ...
    QT_END_NAMESPACE // }

    So I guess what I need is some type of flag option for the QT_BEGIN_INCLUDE_MACROS at configuration time, however I have not been able to find any and if there aren't any then QT is exposing definitions globally without giving the option to namespace them :S

    Thanks for your help.


  • Qt Champions 2016

    @admap
    I did and you're right. Sadly I don't know any workaround for that ... maybe do a build without namespaces and make a namespace-d include?

    namespace Qt {
        #include <QtGlobal>
        // Other Qt stuff
    }
    

    It looks nasty, but might work.

    Kind regards.


Log in to reply
 

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