Solved Console Applications
-
@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.
-
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 themain.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?
-
@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? -
@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!
-
@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
-
Oops sorry I forget that in windows things like Release/ and Debug/ folders. :)
-
@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:
-
@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:
-
@tomy As you cen see the build failed, that's why there is no exe.
-
@tomy Looks like g++ is not found.
Try to add the bin directory of your MinGW installation to PATH and try again. -
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
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. -
@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:
"text.exe" exists but nothing is shown after test.exe in CMD.Update:
After re-opening the CMD and testing the .exe: