Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. cout does not work in Qt
QtWS25 Last Chance

cout does not work in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 9.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Weichao Wang
    wrote on last edited by
    #1

    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

    aha_1980A 1 Reply Last reply
    0
    • W Weichao Wang

      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

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @Weichao-Wang

      Can you show your project .pro file please?

      Thanks

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • W Offline
        W Offline
        Weichao Wang
        wrote on last edited by
        #3

        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

        aha_1980A 1 Reply Last reply
        0
        • W Weichao Wang

          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

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Qt has to stay free or it will die.

          1 Reply Last reply
          4
          • W Offline
            W Offline
            Weichao Wang
            wrote on last edited by
            #5

            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

            aha_1980A 1 Reply Last reply
            0
            • W Weichao Wang

              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

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • W Offline
                W Offline
                Weichao Wang
                wrote on last edited by
                #7

                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

                W 1 Reply Last reply
                1
                • W Weichao Wang

                  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

                  W Offline
                  W Offline
                  wrosecrans
                  wrote on last edited by
                  #8

                  @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.

                  W 1 Reply Last reply
                  1
                  • W wrosecrans

                    @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.

                    W Offline
                    W Offline
                    Weichao Wang
                    wrote on last edited by
                    #9

                    @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

                    aha_1980A 1 Reply Last reply
                    0
                    • W Weichao Wang

                      @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

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      Qt has to stay free or it will die.

                      W 1 Reply Last reply
                      2
                      • aha_1980A aha_1980

                        @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.

                        W Offline
                        W Offline
                        Weichao Wang
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved