简体   繁体   中英

C++ program exits abnormally when outputting integers

I have this question in cout command in my computer. I use MingW and Netbeans for the learning of C++. I tried to compile and run a program with the following code.

#include <iostream>

int main(){
    int x = 5;
    int y = 7;
    std::cout << std::endl;
    std::cout << x + y << " " << x * y;
    std::cout << std::endl;
    return 0;
}

This code is saved as xycalculation.cpp and I compiled it using the command g++ -o xycalc.exe xycalculation.cpp on my Windows XP SP2 computer. I have this environment variables set in PATH. C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;D:\\Qt\\Qt5.0.1\\5.0.1\\mingw47_32\\bin;D:\\MinGW\\bin;D:\\MinGW\\msys\\1.0\\bin

This code compiles fine and creates the xycalc.exe file. and when i try to run it through the command line by xycalc.exe , it gives me a Windows error message and exit without giving any output.

Details of the error, shown when I click the 'For more information about this error, click here' link is following.

AppName: xycalculation.exe   AppVer: 0.0.0.0     ModName: libstdc++-6.dll
ModVer: 0.0.0.0  Offset: 000952a0

Can anyone give me the directions to correct this issue?, it will be a great help for me to continue learning this language.

Thanks in advance.

-static-libgcc-static-libstdc++到命令行以静态链接libs或将它们放在与exe相同的文件夹中。

The reason you are getting this error is because by default your compiler won't compile libstdc++ into your program.

I use codeblocks, but you can replicate the error by using the MinGW compiler and attempting to run the program outside of codeblocks in the console.

To fix this issue you should to add:

-static-libgcc (C) 
-static-libstdc++ (C++) 

to your linker flags which will compile the libraries into your EXE (bigger EXE but it will run).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM