Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

cout does not work in Qt



  • Dear all,
    I've written a very simple test program as below in QtCreator 3.5.1 (Open Source) on Linux. But in QtCreator I cannot have it built. The error during Building are as follows:

    cannot find -lGL
    collect2: error: ld returned 1 exit status

    The program code:

    #include <iostream>
    using namespace std;
    int main(void) {
    cout << "test" << endl;
    return 0;
    }

    cout has no problem when I write the program with a normal editor and compile it with g++. What should I do to let QtCreator build this program? Thanks for any hints!

    Weichao Wang


  • Lifetime Qt Champion

    Hi @Weichao-Wang

    Can you show your project .pro file please?

    Thanks



  • Hi,
    The .pro file is as follows:

    SOURCES +=
    main.cpp

    I've created an empty project with the name sleeptest (I want to test the sleep() function), and added to the project main.cpp, whose content is shown in my original posting.

    Thanks!

    Weichao Wang


  • Lifetime Qt Champion

    Hi @Weichao-Wang

    Try adding:

    CONFIG -= qt
    CONFIG += console
    

    that should help for a console program. If not, please post the compile log here.

    Please note, if you want to compile Qt programs later, you will need to install the GL library. There are multiple threads about that here in the forum and also on google.

    Regards



  • Hi,
    I've done it as you suggested, and build and run in QtCreator works well. Thank you!
    I've just another question. I cannot find the generated executable. I want to start the program at command prompt. Maybe is there settings as where the executable should be put? Thanks!

    Weichao


  • Lifetime Qt Champion

    Hi @Weichao-Wang,

    Creator shows the full path to your exe in the Application Output.

    Reason is, that the build is done outside your sources folder ("shadow building") to keep it clean.

    Regards



  • Hi,
    I've found the place. In the .pro.user file there is indication about BuildDirectory, and it can be changed under Project--> Build & Run. Now I've found the executable here.
    Meanwhile I've tested another project with the project type "Qt Console Application". main.cpp is automatically added to the project, and the .pro file reads as below:

    QT += core
    QT -= gui
    TARGET=TestConsole
    CONFIG += console
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCE += main.cpp

    Here we can see that CONFIG += console is added automatically. Maybe "QT -= gui" has similar effect as "CONFIG -= qt" ?
    In main.cpp I've just inserted the following before "return a.exec()" which is automatically added together with "QCoreApplication a(argc, argv);":
    cout << "test" << endl;

    Now the complete code reads as below:

    #include <QCoreApplication>
    #include <iostream>
    using namespace std;
    int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    cout << "test" << endl;
    return a.exec();
    }

    I can run the program on command prompt, and it outputs "test". But it does not return to prompt and seems to keep running. I must input CTRL+C to return to prompt. How can I modify the code so that the program could return to the command prompt automatically?

    Weichao



  • @Weichao-Wang said in cout does not work in Qt:

    How can I modify the code so that the program could return to the command prompt automatically?

    If you aren't going to use anything from the QCoreApplication, just remove that stuff. "a.exec()" is telling it to enter an event loop and wait until it gets a signal to quit. Normally this event loop is used because you open some windows that you want to stay open, or you start a network service that should stay running. If you don't use any of the Qt features, then you don't need to have QCoreApplication (or QApplication for a GUI) or enter the event loop.



  • @wrosecrans
    Since cout does not work in an empty project, and after inserting CONFIG -= qt and CONFIG += console into the .pro file it works, I came to the idea to test a project of the type console application. "a.exec()" is inserted into the code automatically.
    Now I've removed the include of QCoreApplication, the object QCoreApplication, and replaced the return with 0. It works just fine. Thank you!
    I don't understand why in a console application a.exec() should be inserted. Is there explanation to the difference of empty project and console project in the QtCreator documentation? Thanks for any hints!

    Weichao


  • Lifetime Qt Champion

    @Weichao-Wang

    Qt programs are event driven. If you want to use things like QTcpSocket, you need an event loop running. QCoreApplication is the base for an event driven console program.

    See e.g. http://treyweaver.blogspot.com/2013/02/qt-console-application-template-tutorial.html for a full example.



  • @aha_1980
    You've helped me to solve this problem. Thank you!
    Weichao Wang


Log in to reply