Results 1 to 7 of 7

Thread: Apparently inconsistent compiler behavior

  1. #1
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Apparently inconsistent compiler behavior

    Hi,
    I'm using Qt Creator to build a GUI application in C++.

    Short description: I get different behavior of the application with apparently the same source code.

    Longer description:

    I tried to store some global data in a separate file, "data.h".
    As a test, I defined a string inside a constructor in that file and wanted to set the text of a QLabel in my main window to that string. I later also added an output (both via qDebug and cout) to the constructor to see it it is called.

    I noticed two things:
    - The string didn't show up in the label upon modifying it in the data.h file. I had to clean the project first.
    - The test output inside the constructor was never shown when running the program.

    I also added a qDebug output along with setting the label's text, which was shown and was always consistent with what the label showed.

    I then reproduced the relevant part of code in a small project "Test" to post here (The project is attached). But in the reduced test version I didn't need to clean the project in order for a modified text to appear in the label right away.

    I went back to the main project and noticed that the inclusion of the file "data.h" was in multiples, so I reduced it to just one. Suddenly both problems were gone, even when I undid the change again ! (Except only the qDebug output worked, not the cout.)

    Then I made some changes to the test project, recompiled, undid them again and recompiled again. I don't remember the exact course of action, but the result was that the apparently exact same program version of the test project once exhibited both problems (i.e. for a short time I needed to clean the project first before an updated string would show), and then went back to the way it was (i.e. just the constructor output didn't happen).

    I have no idea what changes in the source code could have been responsible for the different behaviour. To the best of my memory for both projects the exact same source code version showed different behaviour. I can only assume that some other settings were changed implicitly that I'm not aware of.

    Currently, the main project is working all right. Both problems are gone. But given that I have no explanation what either caused them or let them disappear, I expect something similar to happen again and would like to know what I can do about it the next time.

    Is there any automatic setting that can cause something like that ?

    The Test project is attached. For a quick reference, this is the source code of the data.h file:

    Qt Code:
    1. #ifndef DATA_H
    2. #define DATA_H
    3.  
    4. #include <QString>
    5. #include <QDebug>
    6. #include <iostream>
    7. using namespace std;
    8.  
    9.  
    10. class Files
    11. {
    12.  
    13. public:
    14.  
    15. QString f1;
    16. QString f2;
    17. QString f3;
    18.  
    19. Files()
    20. {
    21. f1 = "test orr";
    22. qDebug() << "Files constructor, " << f1;
    23. cout << "Files constructor" << endl;
    24. f2 = "";
    25. f3 = "";
    26. }
    27.  
    28.  
    29. };
    30.  
    31. extern Files files;
    To copy to clipboard, switch view to plain text mode 


    and the data.cpp file:

    Qt Code:
    1. #include "data.h"
    2.  
    3. Files files;
    To copy to clipboard, switch view to plain text mode 

    The main project only differs in that it has more elements, these being a push button and a menu bar, and that it has another UI file for a submenu that pops up from the menu (which I even deactivated, but that didn't do anything).


    Can you please advise me on which part of the build engine can cause such a problem and where to read up on that ?
    Thank you !
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Apparently inconsistent compiler behavior

    Always look at the output of all the tools used to generate files, compile files, link files, ...
    One common problem is that people think that qmake will run all the time. This is not true. If qmake thinks that it doesn't need to alter the makefile, it will not run. In some cases this is wrong.
    Another problem might be that the compiler thinks that some files are not worth preprocessing anymore and it uses the existing object files. Sometimes incorrect.

    How to solve these problems?
    There is only one way to correctly build software and that is from scratch. Always clean the project from object files and generated files and rebuild.

    In the past I made some changes to some KDE code. Before committing the changes I build the code and run the results to see if everything was correct. And everything always was correct before I committed the changes. But then, suddenly, someone complained that the code didn't even compile. How could that be? I checked it.
    So, I double check it, and I compiled the code again (I didn't completely recompile though). Hmmm, strange, it does compile here. So I tell this other guy that he must have made an error somewhere.
    Then another person tells me he can't build the code. I then started looking around what could be the problem. And some of the problems I experienced were that specific files did not get rebuild on my computer, but all these other people were building from scratch and on their computer those specific files did get rebuild resulting in some variables not being available anymore.

    The tools we use are great, but when you build incrementally inside a folder that already contains generated objects and files, strange behavior can and will occur. Always clean the project and rebuild. If you're program is large, split it up in manageable parts.

  3. The following user says thank you to tbscope for this useful post:

    Computer Hater (28th November 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Apparently inconsistent compiler behavior

    I would say (without looking at the actual code) that you forgot to add some file to qmake's HEADERS variable so make doesn't know it should track it as a dependency. And please don't mix the compiler with the whole toolchain.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Apparently inconsistent compiler behavior

    @tbscope:
    Well, I guess I have to read up a lot on the build process.

    @wysota:
    No, in both projects all the used files appear in the project file and all header files appear in the header variable. Actually, I used Qt Creator and they were added automatically.

    I still wonder though why in the Test project the qDebug output inside the constructor doesn't work while it now works in the main project, whereas the cout output doesn't work in either case.

  6. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Apparently inconsistent compiler behavior

    Output goes to the console. In Windows, you don't get a console unless you explicitly request one in your project file, and your output has nowhere to appear. I don't recall the exact syntax - I work mainly in Unix - but this seems to be a common problem. Dig through the qmake documentation for more details.

  7. #6
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Apparently inconsistent compiler behavior

    Well, the thing is that I do get output in both projects when I use qDebug later on, such as when setting up the UI. I just don't get any output in the Test project's constructor of the Files class, whereas the same constructor in the main project now does show output.
    Both project files don't differ except for the number of files included, which in both cases are all the files in the project.
    The project file of the test project is this:

    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2010-11-25T17:13:18
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8.  
    9. TARGET = Test
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. mainwindow.cpp \
    15. data.cpp
    16.  
    17. HEADERS += mainwindow.h \
    18. data.h
    19.  
    20. FORMS += mainwindow.ui
    To copy to clipboard, switch view to plain text mode 

    Anyway, it's just an oddity for now and not a real obstacle, as it works fine in the main project.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Apparently inconsistent compiler behavior

    Quote Originally Posted by Computer Hater View Post
    @wysota:
    No, in both projects all the used files appear in the project file and all header files appear in the header variable. Actually, I used Qt Creator and they were added automatically.
    A respective file has to be referenced in the actual code for make (or actually probably gcc) to treat the referenced file as a dependency of the file containing the reference to the other file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QT inconsistent performance !!!
    By gontham in forum Qt Programming
    Replies: 7
    Last Post: 13th November 2010, 00:51
  2. Strange compiler behaviour (compiler passes wrong argument)
    By SasaVilic in forum General Programming
    Replies: 2
    Last Post: 2nd November 2010, 12:36
  3. Webpage snapshot program is inconsistent
    By adamkauk in forum Newbie
    Replies: 0
    Last Post: 10th November 2009, 21:04
  4. Replies: 37
    Last Post: 15th July 2009, 23:18
  5. Inconsistent behaviour of QVBoxLayout
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2008, 19:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.