3

I am trying to learn C++ now and created a Hello World program. When I compile it on Linux using g++ and it works perfectly fine. When I compile it on Windows using the Build tools, it still compiles the code into machine code, but I can't open the executable. I used the Microsoft build tools as a compiler. The code was:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!";
}

The output should be: Hello, World!

**Question already answered: The program closes because it is not run in cmd. To prevent the program from crashing add

```system("pause");``` 

at the end**

NotTschuschl
  • 85
  • 1
  • 11
  • 2
    ***but I can't open the executable*** Does the executable exist? – drescherjm Nov 30 '20 at 21:05
  • 1
    What do you mean by "open"? – Eugene Sh. Nov 30 '20 at 21:06
  • 1
    ***visual studio installer*** Are you talking about Visual Studio Community? Or VSCode? – drescherjm Nov 30 '20 at 21:06
  • 1
    *What* "build tools"? How do you build it *exactly*? And exactly how are you trying to "open" or run the program? – Some programmer dude Nov 30 '20 at 21:06
  • 1
    To answer this we need to know more about how you are using the build tools and whether or not the build tools execute successfully. – user4581301 Nov 30 '20 at 21:06
  • 1
    There is an output as a .exe file but when I open it, it crashes before you can see the Console Window. – NotTschuschl Nov 30 '20 at 21:07
  • 1
    What do you mean by "crashes"? Does it show an error or just exits immediately? – HolyBlackCat Nov 30 '20 at 21:07
  • 2
    ***it crashes before you can see the Console Window*** I think it just finishes execution in less than a second and ends like any other windows program that finishes. There is a duplicate for how to keep the window open. – drescherjm Nov 30 '20 at 21:07
  • I mean Visual Studio Code but I used the build tools provided by Microsoft – NotTschuschl Nov 30 '20 at 21:08
  • Run `cmd`, navigate to the directory with your `exe` and run it from command line. – Eugene Sh. Nov 30 '20 at 21:08
  • Related: [https://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c](https://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c) – drescherjm Nov 30 '20 at 21:10
  • 1
    FYI, using Microsoft's build tools with VSC is an unusual choice. Normally you use the whole Visual Studio if you want the Microsoft's compiler, OR resort to VSC + MinGW otherwise. – HolyBlackCat Nov 30 '20 at 21:14
  • The question needs to be updated with the information revealed in the comments. Currently there is no logical connection between the issues raised in the question and the answers. – user4581301 Nov 30 '20 at 21:30
  • And in the future, try to include all necesary information in the question itself from the very beginning. If we have to ask you for clarification (a lot), it's a bad question. – HolyBlackCat Nov 30 '20 at 21:36

3 Answers3

6

The executable is showing the correct output on the terminal, but that terminal closes that fast that you don't even realise it.

I'd advise you to open a command prompt, go to the directory where the executable is located and launch it over there. You'll see the desired output.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • ok, but is there a way to prevent it from crashing instantly? – NotTschuschl Nov 30 '20 at 21:10
  • 3
    It's not crashing at all. This is normal behavior. When a program ends execution the window will be closed. – drescherjm Nov 30 '20 at 21:11
  • 1
    @NotTschuschl "Crashing" means failing with an error, it's not the right word here. – HolyBlackCat Nov 30 '20 at 21:11
  • 1
    Nothing is crashing: when you launch an executable, which is meant to be launched in a command prompt, then the command prompt it opened, the executable is launched, and once that executable has finished, the command prompt is closed. The best way to avoid this is by asking for a keystroke or an infinite loop, in order to avoid the executable to stop working. – Dominique Nov 30 '20 at 21:12
2

it possibility because the app closes immediately after ouputing add system("pause");

on the end

ATP
  • 2,939
  • 4
  • 13
  • 34
  • Running another program to pause a program is a pretty brutal way to hold a program open. A simple `cin >> junkchar;` or a debugger breakpoint at the end of `main` would suffice. – user4581301 Nov 30 '20 at 21:30
1

Its not that you can't open that exe file. Once you click on that file, it opens up and does its work and closes. To prevent your exe file from getting closed after doing its operation, you can add following line at the end of the main function :

getchar();

And now once you open your exe file, it wont close automatically, you will need to press "enter" to close it.

#include<iostream>
using namespace std;

int main(){
    cout<<"hello world";
    getchar();
}
Satyam Raj
  • 67
  • 8