[SOLVED] #include does not behave as i thought i



  • Good Evening.
    First steps in Linux C ++ and I'm trying to convert to C ++ an old program written in Visual Basic (version for Visual Studio 10).
    Use QtCreator and Qt, but I think the problem is not of Qt, but some particular C ++ in general do not know.
    If the first part of the main write so:

        #include <QtWidgets>
    
        QMainWindow  *X_mainWindow;
        QMenuBar     *X_menuBar;
        QWidget     *X_centralWidget;
    
        int main(int argc, char *argv[])
    

    and then there is a .h file in which the same variables I declared "extern", the program works correctly.

    As if those same three variables I write to a file "dichiarative_unatantum.h" and I include in the main well:

        #include <QtWidgets>
    
        #include "dichiarative_unatantum.h"
    
        int main(int argc, char *argv[])
    

    in compilation gives me errors like "multiple definition of X_mainWindow".
    Why is this happening? The #include directive should not simulate the insertion at that particular point of the three global variables?
    Give me a hand to understand? Thank you.



  • Hi and welcome to devnet,

    is the file dichiarative_unatantum.h included only once in your program?
    Can you show both that file and the one where the variables are declared as extern??

    TIP: try to avoid to use global/extern variables; they make the code hard to mantain and debug.



  • First of all sorry for my english: I translate from Italian with google translator.
    In the version that does not give error do:

    // main.cpp
    #include "w000_dichiarative.h"
    #include "w001_main.h"
    #include "w010_partita1.h"
    
    #include <QtWidgets>
    
    // #include "w000_dichiarative_unatantum.h" invalidated
    
    QMainWindow  *X_mainWindow;
    QMenuBar     *X_menuBar;
    QWidget     *X_centralWidget;
    
    int main(int argc, char *argv[])
    {
    

    The first of "w000_dichiarative.h" is so:

    // W000_dichiarative.h
    #ifndef W000_DICHIARATIVE_H
    #define W000_DICHIARATIVE_H
    
    #include <QtWidgets>
    
    extern QMainWindow  *X_mainWindow;
    extern QMenuBar     *X_menuBar;
    extern QWidget     *X_centralWidget;
    

    So it works properly.

    But if I substitute the main way:

    #include "w000_dichiarative.h"
    #include "w001_main.h"
    #include "w010_partita1.h"
    
    #include <QtWidgets>
    
    #include "w000_dichiarative_unatantum.h"
    
    // QMainWindow  *X_mainWindow; invalidated
    // QMenuBar     *X_menuBar; invalidated
    // QWidget     *X_centralWidget; invalidated
    
    int main(int argc, char *argv[])
    {
    

    and son-in "w000_dichiarative_unatantum.h" thus:

    #ifndef W_DICHIARATIVEUNATANTUM_H
    #define W_DICHIARATIVEUNATANTUM_H
    
    #include <QtWidgets>
    
     QMainWindow  *X_mainWindow;
     QMenuBar     *X_menuBar;
     QWidget     *X_centralWidget;
    

    compilation gives me the error mentioned before. Why?



  • Hi,

    no problem for the English (I'm Italian too and you can also use the Italia forum to write in your native language).

    Can you post the code? is that header file included only once?



  • Ok Thanks.
    This morning I had asked for help in the forum Linux Mint-Italian, but I had no answer. 2 days ago I wrote two questions on the forum Qt-Italian but nobody deigned to answer me. This is why I wrote in the American forum: you are more prepared and kind.
    The file unatantum.h is inserted only in the main and only in the version that gives error. In the version that works well is not included even in the .pro



  • Oh nooo, I had left a #include "... a tantum.h" in another file. I deleted and now it works. But the initial #ifndef would not have to avoid duplication of definitions? In any case, thank you, thank you very much.



  • Hi,

    I mean this forum.

    BTW, the include guard

    #ifndef XXXX
    #define XXXX
    ...
    #endif
    

    avoids to include twice the same header in a source file but doesn't avoid that you include the same header in two different source file.


Log in to reply
 

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