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

I wrote a simple C ++ program that I compile with g ++ on the command line, and also run it through the command line. My program code is as follows:

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

When I run this code. I get the message "hello_world.exe has stopped working" on my office machine. But when I run the same part of the 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 ++ for code.

UPDATE: I do not run the same binary on both machines. I compile on both machines separately. I use Windows 7 32-bit windows in both places. I am using mingw. To compile, I enter "g ++ hello_world.cpp -o hello_world.exe". To run, I typed "hello_world.exe". I downloaded mingw from mingw.org and used "mingw-get-setup.exe" to install. And I installed g ++ and gcc through the command line using the command "mingw-get install gcc g ++".

+6
source share
4 answers

When you return with main() , your program will stop working. In a gui-based environment, I won’t be surprised if a pop-up message appears saying that the application on the terminal will reach completion, when the user must click “dismiss” before the terminal created to support the application is also interrupted. On Windows 9x Previously, there were such flags in the startup settings for MS-DOS programs.

Questions that you should use to find out the problem: - Does this error message appear if you launch the shell yourself? - Do you use the same binary file on both machines, and if so, then your machines can execute it (for example, do not try to run a 64-bit binary in a 32-bit OS as one of them)

+1
source

This will help to see the exact text of the error message.

Your program depends on the C and C ++ runtime libraries. I suspect that you have libraries installed on the computer where it works, and not where it is not, perhaps because you installed Visual Studio on the computer where you wrote the code, but not on the machine where you trying to run it.

You can install the run-time libraries on the second computer by searching Microsoft Download for vcredist for the version of Visual Studio with which you compiled the program.

+1
source

Works fine for me on Windows 7 32-bit using MinGW. I suspect that you have not installed all the components necessary to run the program. I would reinstall MinGW and Msys and be sure to 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 for Windows for many years (several different versions) and have never had problems compiling, linking, or running standard C and C ++ programs.

+1
source

If you look at http://sourceforge.net/p/mingw/bugs/1678/ , there is a bug related to MinGw and endl. A discussion of this is also interesting.

0
source

Source: https://habr.com/ru/post/954943/


All Articles