C++ HelloWorld program compiled with MinGW crashes with “Illegal Argument”

老子叫甜甜 提交于 2020-01-07 04:05:36

问题


I decided it was time that I learned C++, and after struggling for 3+ hours trying to get the compiler to work, I finally created a working program. However, it seemingly spontaneously broke when I tried to refactor the project in Eclipse by cutting and pasting it. The program simply crashes, and Windows brings up the dreaded dialogue "HelloWorld.exe has stopped working." A bit of debugging revealed that "cout" was considered an illegal argument. I looked some more into the issue, and I'm now suspicious that it has something to do with the compiler apparently being 32-bit, as I have a 64-bit system. The executable is listed in Eclipse as "HelloWorld.exe - [x86/le]." (Minus the period.) My program in full is below:

#include <iostream>

using namespace std;

int main(){
    cout << "Hello World!" << endl;
    return 0;
}

I've also just discovered that creating a new "HelloWorld" C++ project in Eclipse does absolutely nothing to fix the issue, even using the unmodified code and settings. Anyone have any suggestions as to why this would happen?

EDIT: Debugging information: Upon running the program:

Hello World!
Program received signal SIGNILL, Illegal instruction.
0x6fccc3c0 in libstdc++-6!_ZSt4cout ()
    from C:\Windows\SysWOW64\libstdc++-6.dll
(gdb) bt
#0   0x6fccc3c0 in libstdc++-6~_ZSt4cout ()
    from C:\Windows\SysWOW64\libstdc++-6.dll
#1   0x6fc8908c in libstdc++-6~_ZSt4cout ()
    from C:\Windows\SysWOW64\libstdc++-6.dll
#2   0x004013be in libstdc++-6~_ZSt4cout () at HelloWorld.cpp:4
(gdb)

It should be noted that line 4 of the class now points to the cout call.


回答1:


After looking at your gdb backtrace, the problem appears to be an incompatible C++ runtime libstdc++.dll.

This could happen if you're installing MinGW over an existing install. Another way this could happen is if some other third party program needing libstdc++.dll installed its dependencies into your Windows\SysWow64 so it could be found system wide. The issue is of course, differing versions of the libstdc++ aren't compatible with each other at the ABI level. Programs compiled with a given Mingw g++ version needs to load the corresponding libstdc++.dll that came with that particular Mingw install.

Open a new cmd.exe prompt and set the Path environment to your current mingw\bin install directory. For example, if your mingw install is in c:\mingw32-4.7.2:

C:\>set path=C:\mingw32-4.7.2\bin

Then try running helloworld.exe again. If it runs to completion without crashing then that is likely the problem. In this case you should remove the libstdc++.dll from windows\syswow64.




回答2:


Build it statically so you don't have to worry about wrong depencies. Just add "-static" to your Compiler-options.



来源:https://stackoverflow.com/questions/18157464/c-helloworld-program-compiled-with-mingw-crashes-with-illegal-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!