Console Applications



  • Hi all,

    through this wizard:
    File > New file or Project > Application > Qt console application, I created a project and wrote this into main.cpp:

    #include <QCoreApplication>
    #include <qtextstream.h>
    
    QTextStream cout(stdout, QIODevice::WriteOnly);
    
    int main(int argc, char** argv)
    {
        // avoid compiler warnings
        Q_UNUSED(argc)
        Q_UNUSED(argv)
        QString s1("Paris");
        QString s2("London");
        // string concatenation
        QString s = s1 + " " + s2 + "!";
        cout << s << endl;
    }
    

    When I run it using Desktop kit, it shows nothing!
    What is the reason please?



  • The result of Qt console application is available inside qtcreator

    in application ouput

    As described in this sample image

    alt text



  • Good, thank you.
    Where are these kinds of apps used and useful?



  • @tomy said in Console Applications:

    Good, thank you.
    Where are these kinds of apps used and useful?

    If you want to have an application without any Human machine interface



  • Does it mean an app in communication with another app/hardware (without any show to the owner of the machine/app/hardware), like in a refrigerator?



  • @tomy said in Console Applications:

    Does it mean an app in communication with another app/hardware (without any show to the owner of the machine/app/hardware), like in a refrigerator?

    I am not sure,but i think yes

    This is what wikipedia says about Console application

    https://en.wikipedia.org/wiki/Console_application

    ...For data processing tasks and computer administration..

    Maybe expiremented people ,can enlighten us a bit more ?



  • @tomy No... console apps are applications your run on the console/terminal.

    In linux a lot of applications are console only. No GUI.

    Console applications can have a UI just not a GUI.

    Think about almost any command you've ever run, it's a console application.

    Things like tar, ls, cp, rmdir, etc are all console apps.

    Most utilities provide a console method with their applications. So things like winzip will have their gui mode but also a console mode. This helps in automation and running applications from batch scripts, etc.

    Even the compiler you use in Qt Creator is a console application.

    I work on the command line (console) more than I actually do in an IDE like Creator. Almost all development tools are console only. Cmake, mingw, gcc, ldd, gdb, etc. Cmake does have a GUI for windows but on other platforms it's all console.

    Hope that helps you understand the difference.



  • @ambershark

    No... console apps are applications your run on the console/terminal.

    Like CMD on Windows, or when I run qmake, binarycreator .exe file by CMD, yeah?

    In linux a lot of applications are console only. No GUI.

    Console applications can have a UI just not a GUI.

    Yes, I've written many many console apps in C++ so far, but I was curious to know their application in the real world.

    Think about almost any command you've ever run, it's a console application.

    Things like tar, ls, cp, rmdir, etc are all console apps.

    I don't know these extensions :-) but I got your mean.

    Most utilities provide a console method with their applications. So things like winzip will have their gui mode but also a console mode. This helps in automation and running applications from batch scripts, etc.

    Does it mean this provide the user of of such an app with "two" ways of executing a given app? If so, why should the programmer bother himself for that extra way!?

    Even the compiler you use in Qt Creator is a console application.

    And does it mean that the Qt Creator has two parts: an IDE (which is visual) and we work on it, and a practical and functional part which we don't deal with it directly but it does the work?

    I work on the command line (console) more than I actually do in an IDE like Creator. Almost all development tools are console only. Cmake, mingw, gcc, ldd, gdb, etc. Cmake does have a GUI for windows but on other platforms it's all console.

    Ow great.
    A funny story: When I was always writing console applications (all the drills, exercises and try-this-s of the C++ programming book of B.Stroustrup), I didn't like them and would think that the only benefit of these hard-working on writing these apps is to make us ready to use them on GUI apps!

    Hope that helps you understand the difference.

    It helped much and thank you. :-)



  • @tomy Yea exactly like cmd on windows. That is the console for a windows platform.

    Real world console applications are things like utilities, servers, etc. Some you might know in windows are ipconfig, ping, net, etc.

    Those "extensions" are all applications in a posix environment. Linux/osx/android/etc.

    I can give you a real world example for the multi-UI approach... Recently I was contracted to write a program that handled packaging of files for distribution to embedded devices like credit card terminals, etc. We'll call it package manager.

    This package manager had a GUI where you could build the packages easily with drag and drop from the filesystem, etc. It handled all the devices types you could create the packages for, and generally was easy to use as GUIs tend to be.

    However, there was a build and automation team at this company who needed to make these packages as well. For them I provided the same functionality but in a CLI (command line interface AKA a console application). This allowed them to automate creating of packages with their ruby scripts. Also it allowed the SCM build management team to create packages as parts of the build for customers all from cmake and other automated build environments. Qmake could do it as well.

    So basically, GUIs save a lot of typing work, but require human interaction. CLIs you can pass the information in via a file or on the command line, however you want, and it's automated. So it can all be run as part of an automated thing.

    There are a lot of times where I prefer console apps to GUI ones. Most people who want to mess with files on their system would open a file manager (in windows Explorer), click around to browse to their files, etc. For me I prefer the console. I would open a terminal, cd to the directory I need, cp to copy files, mv to move, etc. I much prefer typing to a mouse. A mouse is slow, typing is very fast, especially if you know hot keys for everything.

    A good example of this.. let's say you wanted to know your IP address (in windows)... With a keyboard not even touching your mouse you could hit Window+R type cmd hit enter. Then type ipconfig /all<enter> and you're done. That's using the CLI. To do the same thing in the GUI, you have to click start, control panel, Network Settings, Right click your adapter, properties, double click TCP/IP v4, and finally you have your IP address... It would take me about 3 seconds with a keyboard and upwards of 15-20s with a mouse.

    And finally yes, most (all?) IDEs are just ways to wrap the underlying console tools. So Qt Creator uses mingw or visual studio as it's compiler, and gdb (or vs?) as it's debugger. Everything you do in Qt Creator can be done (very easily) without it. You can do it on the command line, or you can use other IDEs.

    As a die hard vim user I prefer VI compatible IDEs like slickedit or clion (those aren't free though). Creator has some basic vim support which is cool, as does visual studio. CLion is my favorite these days though. It doesn't have support for Qml at the moment which annoys me but I think when I get time I will write a Qml plugin for it, assuming they don't beat me to it. ;)

    A final example... in Qt Creator you would hit build, underneath here is what it would do with a hidden console:

    qmake
    mingw32-make -j4
    

    So you could do that on your own without the IDE if you wanted. This is more prevalent in osx or linux than in windows though. Windows command line stuff is annoying rather than helpful like it is in a posix environment.



  • @tomy said in Console Applications:

    Does it mean an app in communication with another app/hardware (without any show to the owner of the machine/app/hardware), like in a refrigerator?

    This is called embedded software. SW + HW like an "intelligent" refrigerator is an embedded system. Console applications can be command line applications which means you write one command (with command line arguments) and the application runs without further interaction. A console application can also have a TUI, text based user interface (see the wikipedia article mentioned above) using for example ncurses library. Or it can be line based interactive like many C++ tutorial applications, writing output line by line and receiving text input one line at a time.

    Actually many of the real life GUI applications have some console features. If nothing else, you can start it with an argument like "--version" (many GNU/Linux apps) or "/?" (many Windows apps) and they output the version number or short help. Many apps can take any file name as an argument and try to open it as if you chose the file from File->Open. In your own applications command line arguments can be very useful in development phase, you (or a test user) can give --debug argument and your main() code switches debugging with qDebug on. Or you can switch features on/off without changing the code and rebuilding. In Qt Creator open Projects->Run and you can add Command line arguments there.

    In your program you can handle command line arguments with QCommandLineParser, or in simple cases just get them with QCoreApplication::arguments() and handle the string list.



  • @ambershark

    I agree that these console uses are very prevalent in mac os and linux and you have been on linux and it has made it that you prefer console to mouse clicks.
    I think windows command line stuff is not very annoying (and you used it to demonstrate the ability of a cmd compared to mouse clicks in the IP example!)

    Lets teach me (!) using a CLI instead of the IDE in Qt Creator:
    As shown above, I've created a console project (in the first post of the thread), then wrote a simple code in the main.cpp.
    Now in the IDE I can click on run (or ctrl+r) to see the result. Then it finishes. How to do these things (this process) using CLI without using the IDE?

    As an out of scope question: do you return the base of CMD (DOS OS) to Unix?


  • Moderators

    @tomy "As an out of scope question: do you return the base of CMD (DOS OS) to Unix?" - what does it mean? What is base?

    Writing a simple CLI C++ app without IDE in a terminal (CMD):

    • Use an editor to write your cpp file (for example vim or what ever): vim myapp.cpp
    • Then call the compiler: g++ -o myapp myapp.cpp
    • Then start your app: ./myapp
      That's all for a simple app.
      In Qt creator you can see what it does if you press CTRL-R in "Compile Output" tab: it uses only CLI tools, like qmake, compiler (for example g++), linker,...

    Console apps are not only important on UNIX/Linux/MacOS but even on Windows. You need them for example to write scripts, build software,...



  • @tomy @jsulm's example is perfect for linux but since I know you don't know linux yet and are using windows I'll give you the same thing for windows:

    Launch a cmd and do the following:

    > mkdir test
    > cd test
    
    create a main.cpp with somthing like:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       cout << "hello world" << endl;
       return 0;
    }
    
    > qmake -project
    > qmake
    > mingw32-make
    > test.exe
    

    This assumes you have /path/to/qt/bin in your path variable in order to use and access qmake. It also assumes you have mingw32 compiler installed. If not substitute compilation command mingw32-make for whatever compiler you have.

    This is a great example of where linux is easier than windows on a command line (from our other thread). There is no real additional setup in linux, all the stuff @jsulm wrote works right out of the box.

    Edit: Explaining some of the commands --

    qmake -project Will create you a test.pro file. This is only done once to bootstrap your project file. After that you modify that .pro file to add the things you need.

    qmake evaluates the project file and creates a makefile for you.

    mingw32-make invokes the make program for the mingw compiler. This will execute the Makefile that was generated when you ran qmake and build your application using mingw32-g++, and other pieces it needs to compile and link.



  • Thank you very much.
    One question:

    "> test.exe"

    Why should we have a test.exe file?


  • Qt Champions 2016

    @tomy
    Hi
    The test.exe comes from the executing the real makefile as neatly explained
    in section "mingw32-make " :)



  • Hi,

    I asked because I don't have that file in the test folder!


  • Qt Champions 2016

    @tomy
    Well do you see .o file ? Maybe there is a build folder one level up?
    If not, then show the log (text) you get from running mingw32-make (step)
    Maybe there is compile or link error as that would do that no .exe is created.

    Ah, sorry. Its in the release folder

    alt text



  • Oops sorry I forget that in windows things like Release/ and Debug/ folders. :)


  • Qt Champions 2016

    @ambershark
    Well i had to follow the sample and see. Was not sure what would really happen :)



  • @mrjj Lol. I'm glad it actually worked since I just typed that out without testing anything. And I don't use windows much so I could easily have messed it up. :)



  • Sorry there isn't such a file an any folder there:
    alt text


  • Moderators

    @tomy Maybe it is just called differently? So, is there ANY *.exe file?
    Also, if you build in CMD you actually will see which files are generated. Did you check?



  • Look, I did these:
    Creating a C++ file named "main" with a simple code in it, in the "test" folder.
    Then, found and ran all three commands (qmake -project, qmake, mingw32-make).
    And the result as shown above (with no ".exe" file in the "test" folder.
    I CMD:
    alt text


  • Moderators

    @tomy As you cen see the build failed, that's why there is no exe.


  • Moderators

    @tomy Looks like g++ is not found.
    Try to add the bin directory of your MinGW installation to PATH and try again.


  • Qt Champions 2016

    Hi
    An alternative to fiddling with path is to run the
    c:\Qt\5.8\mingw53_32\bin\qtenv2.bat
    in the cmd before trying to compile.



  • @mrjj

    Hi
    An alternative to fiddling with path is to run the
    c:\Qt\5.8\mingw53_32\bin\qtenv2.bat
    in the cmd before trying to compile.

    Hi,
    "The system cannot find the path specified."
    Anyway, it's not that important and we can leave it out because it's not my purpose to be familiar with running files from CMd, now. Maybe when needed.
    (I liked to test that simple example this way but, the testing may not be so easy) Thanks.


  • Moderators

    @tomy Depending on the MinGW version you install the path can be a bit different. Just search for qtenv2.bat file in your Qt installation directory.



  • Yes, I did it and I think the system is set now and ready for the next tests. You can look at this:
    alt text
    "text.exe" exists but nothing is shown after test.exe in CMD.

    Update:
    After re-opening the CMD and testing the .exe:
    alt text



  • @tomy
    what code you write in test?



  • @tomy Locate the libgcc_... file on your system and copy it to the same directory with the exe. Or set the PATH variable. See http://stackoverflow.com/questions/4702732/the-program-cant-start-because-libgcc-s-dw2-1-dll-is-missing.


  • Qt Champions 2016

    @tomy said in Console Applications:

    "The system cannot find the path specified."

    Well you need to use the correct path for your installation.
    It sets the correct path for tools but that you can also do in other ways.


  • Qt Champions 2016

    @tomy
    You need to provide the DLLs it wants from the CORRECT compiler folder under c:\Qt
    so you end up with (maybe more DLLs)
    alt text

    Please read
    http://www.tripleboot.org/?p=138



  • @mrjj

    You need to provide the DLLs it wants from the CORRECT compiler folder under c:\Qt
    so you end up with (maybe more DLLs)

    I added them from the folder: C:\Qt\Qt5.8.0\5.8\mingw53_32
    No reaction.


  • Qt Champions 2016

    @tomy
    Did you read the link ?
    Anyway, it might need extra dlls :)
    You can try the tool
    http://doc.qt.io/qt-5/windows-deployment.html

    or use
    http://www.dependencywalker.com/
    To check the dlls.

    Anyway, to make it run, that release folder must be an deployment folder so keep reading docs and it will work :)



  • At the beginning there were only four simple CLI commands to get the result on our contraction, but now I see after doing more than 4x commands I haven't the file run, and still need to go on!
    Why should we bother themselves this way! In the Qt Creator or Visual Studio (for console apps) we can get the result in 10x faster than this CLI method.

    CLI may be faster in some cases, but not in this one, at least!
    Thank you all. Let's don't continue the topic. Thanks again.


  • Qt Champions 2016

    @tomy
    well being experienced means you know for each job what is the right tool.
    Besides using GUI or CLI is 2 sides of same coin. If you look in
    Creator/VS you can see the command line it uses to get the job done.

    So if i need CLI action i will visually create and edit files and then simply steal the CLI line to build it.

    That way i can use CLI when it suits :)

    Its not fair to dismiss CLI as GUI would be nothing without it.

    GUI was invented to make life easier for those of us that feels CLI is just too much typing but i work with ppl that can create something 10X faster using CLI. Building libs and dependencies. They like CLI for the direct
    power user control it gives. If its not for your liking ( or mine ;) then its not the tools fault in my opinion.

    The reason it just dont work for you is that the environment is not setup and u are used to the IDE setting it up before running exe. The qtenv2.bat was part of that automation etc. So power user will bother to use CLI as its
    efficient and there is noi layer in between (the IDE) so they prefer CLI and some kinky editor :)



  • @tomy said in Console Applications:

    At the beginning there were only four simple CLI commands to get the result on our contraction, but now I see after doing more than 4x commands I haven't the file run, and still need to go on!
    Why should we bother themselves this way! In the Qt Creator or Visual Studio (for console apps) we can get the result in 10x faster than this CLI method.

    CLI may be faster in some cases, but not in this one, at least!
    Thank you all. Let's don't continue the topic. Thanks again.

    Oh yea CLI is faster in some cases not all. The reason I recommended you trying the CLI for a console app is to learn this stuff. But I bet if we raced, me on a CLI, you with the IDE, I'd win every time. ;) I'm not saying don't use an IDE, they are great. But you should at least understand what the IDE is hiding from you (or making easier). Like @mrjj said GUIs are just making life easier for people who want to type less. And they usually do a great job. I use an IDE too, but not always. For a lot of things I use the CLI since it's so much faster. Git for instance. Almost always command line unless there is a complex command, then I'll use a gui.

    All the problems you're running into here are things you will need to know how to deal with. If you were to distribute any software you would need to deal with all these dlls to get it to run on someone else's machine. This is normal development stuff that the IDE is hiding from you. It's very important to learn.

    This is also really only an issue with windows. Windows has (at least in my opinion) really annoying ways of finding and using libraries for an application. On a linux or mac system with a properly set up Qt environment, the binary would just run without needing to be pointed at the right supporting libraries, or having to make a distribution by copying the libs to your app directory.

    Well, not entirely true, posix OSes have these issues as well but it's really only when you need to make a release distribution and not during development. Even on the command line.



  • Yeah, of course you would win :-) ;)

    GUIs are just making life easier for people who want to type less.

    All people (except professionals) like this way; and since I want to be a professional on this case too, I will certainly catch this topic. I think when I will start using Linux, it will be a good point to learn it too, or even by using the Linux terminal it will be easily achievable.

    If you were to distribute any software you would need to deal with all these dlls to get it to run on someone else's machine.

    I've done this before. :-)
    When I using my C++ experience became able to write a good accurate Qt GUI calculator while I'd read only the first chapter of the Qt book (a few pages), I went to make it installable on others' machines and went to the end of the process and by Qt Framework Installer the goal was available. ;)

    So, I'd gone the way, "but", I wouldn't like to re-go through such a rather complex way for such a simple code (main.cpp). :)

    Windows has (at least in my opinion) really annoying ways of finding and using libraries for an application.

    :-) :D
    Thanks.



  • @tomy said in Console Applications:

    I think when I will start using Linux, it will be a good point to learn it too, or even by using the Linux terminal it will be easily achievable.

    Definitely a good time to learn command line stuff. Linux is a command line OS. You can install a window manager and run GUI apps but it's not a native linux thing. It's a completely separate program.

    You don't necessarily need to learn the command line stuff to succeed as a professional developer, but it will help a lot. And you will never be as good as someone else who knows it.


Log in to reply
 

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