Somehow display C++ application using double-click on Ubuntu? - c++

I'm totally new to Ubuntu and C++. Anyway, I have PHP experience.
I just created very simple application...
#include <iostream>
int main() {
std::cout << "Hello, world!";
return false;
}
Then compiled it....
g++ hello-world.cpp -o hello-world
But I can't open it with double-click on it, like I did on Windows 7. Only way to get that text printed is to do command...
./hello-world
Is it possible to open compiled file using simple double-click and then get that text somehow printed?

The program you wrote is a console application. In most Linux GUIs, by default if you open a console program from the GUI, the console output will not be displayed. You can either configure the GUI to open a terminal, or you can manually open a terminal and run it yourself.
When doing development, I highly recommend manually running the program - with using the GUI's automatic terminal window opening mode, the terminal will close as soon as the program terminates; so if the program crashes, the message will be lost. Manually opening a terminal ensures it sticks around after termination, so you can read the program's last messages before terminating.

Related

QT Creator Can't read from stdin in debugger

I just updated QT Creator to 4.13.0 and now I can't read from stdin while I'm in the debugger.
A few details...
I am running on macOS.
I am writing C++, but using C stdio library. (for historical reasons)
I am using clang 15.5.0 in c++11 mode.
I am using the 'Run from Terminal' debugger option.
Problem: Type as I might, when my code goes into fgets it never returns.
Simplified Example:
#include <stdio.h>
int main()
{
char buf[1024];
if (fgets(buf,1024, stdin)) // In debugger, this call never returns!
{
fprintf(stderr, "%s", buf);
}
return 0;
}
Note: When I run this outside of the debugger - no problems.
Has anyone run across this and fixed it? Any ideas on how to fix it?
Could be an issue with the terminal start application openTerminal.py inside QtCreator.
I had a similar issue, where the output of my console application appeared correctly in a terminal session when starting in debug mode without the debugger, but when starting using the debugger all system output and system error was written to Application Output widget making it impossible to read any values from std::in.
Solution was:
Manage Kits: Environment System Replace the long path ".../openTerminal.py"
by "/Applications/Utilities/Terminal.app"

No console application on Linux

On Windows I normally create a Windows Desktop Application, this is because console applications display a brief black box on the screen.
I am using CodeBlocks on Linux Mint, how could I do the equivalent of the above but on Linux?
I do not want to hide the terminal window after it has been displayed.
Linux does not have the same "subsystem" concept as windows: there is no difference or separation between console and desktop applications. When you start an application on Linux, it will not open a console window, unless the programmer explicitly programmed it to open one.
If the application writes anything to stdout or stderr, what happens with that depends on how exactly the application got started. By default, the application inherits the stdout and stderr of its parent process. If the application is being started from a terminal, the output will be visible on the terminal. If the application was started by the desktop environment from a menu entry, the output may go to a log file or it may be lost.
If you see a terminal window open when you run your program from the IDE, that's something that the IDE is doing for you, it's not your application. If it bothers your, I would think that the IDE has a way to disable this behavior in settings.
Look into QT. It is a GUI framework that works on Linux.
You can write your code without creating a main window (or maybe you have to have a main window but it can be always hidden... it's been a while since I've used it).
Be aware though, that you may run into usability issues with this type of design... the user has no way of knowing if your app was launched or if it succeeded, when it's completed, etc.
The simple way is to use (e.g.) xterm [or gnome-terminal] to get a terminal window.
Then, invoke your program from the shell [manually]:
/path_to_my_program
You may be able to configure codeblocks to do this for you.
Or, you could add some code that holds the default window open:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid = fork();
if (pid != 0) {
waitpid(pid,NULL,0);
while (1) sleep(1);
}
long double n;
n=5;
printf("n= %Lf\n",n);
return 0;
}
UPDATE:
The invocation command can be controlled from: Settings -> Environment -> General Settings
The default is to invoke in an xterm sub-window [popup]. You may be able to change the settings to (re)use an existing [terminal] window.
Note that [a codeblocks program] cb_console_runner is used. You may be able to replace this with something more to your liking.
I do not want a GUI nor a terminal popup...
You'll need some sort of terminal to run the command in. This could be a script that diverts stdin/stdout/stderr as appropriate [and suppresses the invocation of a sub-window], so you'll have to experiment a bit.
As I mentioned above, you can just open a terminal window outside of codeblocks and then run the command manually inside it. Technically, this is not a popup. But, you lose the [automatic] debugger invocation.

Visual Studio .exe files open and immediately close

I have a problem, when I try to open my Hello World.exe file (that I created by following a tutorial). It immediately closes without giving me the chance to read or see if I have done everything correctly.
As you can see, I need help on how to keep it open, without instantly closing.
You can either put a break point before the end of main or try the following:
int main()
{
//...
std::cin.get();
return 0;
}
It is going to wait for you to press some key to exit the console.
EDIT: It is better to add break point which do not change existing code.
In console applications there are a couple of things you can do to stop the window from closing on you such as using system("pause") (not so recommended though), getch(), std::cin >> x etc at the end of the application.
Another option is to start a cmd window, cd to the location of the exe and run it like any other console application is meant to be ran, that way it wont just close on you, it'll simply exit.
In VS2017, you can specify that the executable is a Console app, not a Windows app. This makes your application run in a "Microsoft Visual Studio Debug Console", with "Press Any Key To Close This Window" appearing at the end of the console output:
Right click the project to bring up Properties.
Linker > System > Subsystem > Select "Console".
Press Apply before closing.

C++ on windows closes a program immediately after launching

I installed minGW and the eclipse CDT, and the console keeps doing something weird. The code of the program is
using namespace std;
#include <iostream>
int main() {
cout << "Hello, windows (8, c++)" << endl;
//system("PAUSE");
return 0;
}
You all know it, its the Hello World program. Now when I run this the Eclipse console displays some stuff about building, and then goes blank. And when I navigate to the HelloWorldProgram.exe in the explorer and run it, a windows flashes up and displays "hello world", but then immediately closes. When I do this on Mac OSX there's no problem, and the windows stays up until I decide to close it. Now I know there's a command
system("PAUSE") //I dont know what I need to import to use this. Could you tell me that too?
Which will give me more or less the same effect, but I'd like to know why Windows does it differently from OSX, and what I can do to fix it (bc this annoys the crap out of me).
Looking forward to your replies!
This happens on Windows because this is just the behavior of the Windows console. You'll have to open up the console manually and then running your program through the console you've opened if you don't want the window to close automatically once the program has executed.
You may want to take a look at these:
What is the Best Practice for Combating the Console Closing Issue?
https://superuser.com/questions/186562/how-can-i-keep-the-terminal-open
Don't use system("pause"), it's wrong for a multitude of reasons (read more about it here).
Put cin.get() before return and the window will stay open until you press enter.
If you want to just run your console program, you should open a console, and run it.
Apparently, the OSX version of Eclipse is configured to open a console, and run the program, and not close it. Maybe you can configure the Win version so, too.
You shouldn't meddle with your program to behave differently on another platform, instead wrap it into something that 'adapts' the behaviour.
Probably, you can tell eclipse to use "cmd /c 'yourprogram.exe && pause'", to open a command window and have it execute your program and then pause.
Just add getch(); before return, and add #include <conio.h>.

C++/G++ hello world application issue, maybe compilation

I am trying to compile my first c++ file on windows with the g++ compiler...
My cpp file is the following -
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
I type in this on command prompt to get to the directory
cd C:\Users\Mark
Then to compile my program I do
g++ hello.cpp
It creates a file name a.exe (default) but when I click on it the command prompt quickly flashes open then it's gone.
What did I do wrong? It should say Hello World! on the prompt and stay there, right?
What you did looks correct, but if you start the program by double clicking it in Windows, the new command prompt will be closed once it is finished. Since you already have a command prompt open for compilation, try to start the program from there, too:
g++ hello.cpp -o hello.exe
hello.exe
That's just normal Windows behavior of executing a command-line file from the UI -- as soon as the program exits (immediately in your case), the prompt window closes.
Running it from the command line will keep it up. To do that, Start > Run > cmd, then cd to the directory, then type a.exe.
Alternatively, you can wait for input from the user to keep the window open. That's fine for testing, but nobody who actually executes programs from the command line would want to have to hit a key to stop a program from running, when it should exit on its own correctly.
you didnt do anything wrong. There isnt a readline or anything at the end of your program, so when it is finishes executing it closes. Add a read character at the end of the program to make it wait for input to terminate.
You did everything right. Whether or not the command prompt stays open has nothing to do with your program. The command prompt is itself a program, and its default behavior is that when it launches in response to the execution of a console application, it closes immediately when the program is finished.
What you should do is launch the command prompt yourself and then run the program from within it, rather than launching the program by double-clicking its icon.
Do not fall into the bad practice of adding a call to some sort of pause function, or waiting for input at the end of the program. You should not get in the habit of hard-coding work-arounds for unwanted behavior of a particular shell into your application. Just use the shell the right way to get the behavior that you want.
The prompt has nothing to do with your program or the language. It has to do with your OS.
Your program should be run from the command line, which will (obviously) leave the command window up when it's finished.
There are tricks to make it stay up if you really want, but these are just tricks, and not necessarily good practice. One would be:
int main()
{
std::cin.get(); // waits for enter
}