Shaun
Shaun

Reputation: 313

Why does my C++ program crash on one machine and not on another?

I have written a simple C++ program that I am compiling using g++ in command prompt and also running it through command prompt. The code of my program is as follows:

#include<iostream>

int main() 
{
    std::cout<<"Hello world"<<std::endl;
    return 0;
}

When I run this code. I get a "hello_world.exe has stopped working" kind of error on my office machine. But when I run the same portion of code at home it works fine. Any idea why this is happening? Also, if I remove std::endl it works fine.

I am using Notepad++ to code.

UPDATE: I am not running the same binary on both machines. I compile on both the machines separately. I am using windows 7 32-bit at both the locations. I am using mingw. For compiling I type "g++ hello_world.cpp -o hello_world.exe". For running I typed "hello_world.exe". I downloaded mingw from the site mingw.org and used the "mingw-get-setup.exe" to install. And I installed g++ and gcc through the command prompt using the command "mingw-get install gcc g++".

Upvotes: 7

Views: 3685

Answers (3)

user1356386
user1356386

Reputation:

Works fine for me on Windows 7 32-bit using MinGW. I suspect that you've not installed all the components you need to run the program. I would re-install MinGW and Msys and be sure you install all the necessary C and C++ components.

g++ --version
g++.exe (GCC) 4.6.2

a.exe
Hello World

I have used MinGW and Msys on Windows for many years (several different versions) and have never had issues compiling, linking or executing standard C and C++ programs.

Upvotes: 1

Adrian McCarthy
Adrian McCarthy

Reputation: 47954

It would help to see the exact text of the error message.

Your program depends on C and C++ runtime libraries. I suspect you have the libraries installed on the machine where it works and don't where it doesn't, probably because you installed Visual Studio on the machine where you wrote the code but not on the machine where you're trying to run it.

You can install the runtime libraries on the second machine by search Microsoft Download for vcredist for the version of Visual Studio that you compiled the program with.

Upvotes: 1

PypeBros
PypeBros

Reputation: 2646

When you return from main(), your program stops working. In a gui-based environment, I wouldn't be surprised to see a pop-up message warning about a terminal-based application reaching completion where the user has to click "dismiss" before the terminal spawned to support the application is terminated as well. Windows 9x used to have such checkboxes in launcher preferences for MS-DOS programs.

Questions you should use to find out the issue are: - Is it showing the same error message if you launch the shell yourself ? - Do you use the very same binary on both machine, and if so, are your machines both capable of running it (e.g. not trying to launch a 64-bit binary on a 32-bit OS as one of the case)

Upvotes: 1

Related Questions