C++ 如何解决 -------undefined reference to `__chkstk_ms'-------on mingw

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18649173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:05:58  来源:igfitidea点击:

How to solve -------undefined reference to `__chkstk_ms'-------on mingw

c++linkerg++mingw

提问by Yunus

I have just install gcc and g++ on mingw. I wrote a very simple "hello world" program to test if the g++ compiler worked. Code:

我刚刚在 mingw 上安装了 gcc 和 g++。我编写了一个非常简单的“hello world”程序来测试 g++ 编译器是否工作。代码:

#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}

Compile:

编译:

g++ Test.cpp

And then some errors appear:

然后出现一些错误:

D:/MinGW[Finished in 0.6s with exit code    1]/bin/../lib/gcc/mingw32/3.4.5/../../../../mingw32/lib/crt2.o: In function `_mingw_setargv':
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../mingw32/lib/libmingwex.a(glob.o): In function `glob_match':
i:\p\giaw\src\pkg\mingwrt-4.0-1-mingw32-src\bld/../mingwrt-4.0.0-1-mingw32-src/src/libcrt/misc/glob.c:733: undefined reference to `__chkstk_ms'
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../mingw32/lib/libmingwex.a(glob.o): In function `glob_strdup':
i:\p\giaw\src\pkg\mingwrt-4.0-1-mingw32-src\bld/../mingwrt-4.0.0-1-mingw32-src/src/libcrt/misc/glob.c:85: undefined reference to `__chkstk_ms'
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../mingw32/lib/libmingwex.a(glob.o): In function `glob_match':
i:\p\giaw\src\pkg\mingwrt-4.0-1-mingw32-src\bld/../mingwrt-4.0.0-1-mingw32-src/src/libcrt/misc/glob.c:841: undefined reference to `__chkstk_ms'
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../mingw32/lib/libmingwex.a(glob.o): In function `glob_strdup':
i:\p\giaw\src\pkg\mingwrt-4.0-1-mingw32-src\bld/../mingwrt-4.0.0-1-mingw32-src/src/libcrt/misc/glob.c:85: undefined reference to `__chkstk_ms'
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../mingw32/lib/libmingwex.a(glob.o):i:\p\giaw\src\pkg\mingwrt-4.0-1-mingw32-src\bld/../mingwrt-4.0.0-1-mingw32-src/src/libcrt/misc/glob.c:85: more undefined references to `__chkstk_ms' follow
collect2: ld returned 1 exit status

回答by koczis

The orig problem seems solved regarding linking against old libs, but I have opposite case. I need to work with older GCC 3.4 version, so I installed gcc-v3-core package. Then orig errors immediately appear when linking a trivial source. I found out that MinGW GCC 3.4 cannot work with mingwrt > 3.20 and the problem is that gcc-v3-core contains incorrect mingwrt spec, allowing newest vesions.

关于链接旧库的原始问题似乎已解决,但我有相反的情况。我需要使用较旧的 GCC 3.4 版本,所以我安装了 gcc-v3-core 包。然后在链接一个微不足道的源时立即出现原始错误。我发现 MinGW GCC 3.4 不能与 mingwrt > 3.20 一起工作,问题是 gcc-v3-core 包含不正确的 mingwrt 规范,允许最新版本。

The solution is simple:

解决方法很简单:

mingw-get upgrade "mingwrt=3.20.*"

!Update! Actually with mingwrt-3.20 linker reports same errors for C++ code:

!更新!实际上使用 mingwrt-3.20 链接器报告 C++ 代码的相同错误:

g++ hello.cpp

BAM!

砰!

Solution 2:

解决方案2:

mingw-get upgrade "mingwrt=3.18.*"

the last one that seems to work with gcc-v3-g++.

最后一个似乎适用于 gcc-v3-g++ 的。

回答by Keith Marshall

Here's an authoritative answer, from a MinGW project administrator.

这是来自 MinGW 项目管理员的权威答案。

Your problem arises because you continue to use an obsolete, (no longer maintained; no longer supported), version of GCC. Current versions of mingwrtare compiled using GCC-4.x, (I used GCC-4.8.2 for mingwrt-3.21and its descendants), and this introduces the dependencies on __chkstk_ms, (which is provided by libgcc-- a GCC-4.x specific library, which is incompatible with GCC-3.x).

您的问题出现是因为您继续使用过时的(不再维护;不再支持)GCC 版本。当前版本mingwrt是使用 GCC-4.x 编译的(我使用 GCC-4.8.2mingwrt-3.21及其后代),这引入了对__chkstk_ms,的依赖(由libgcc-- 一个 GCC-4.x 特定库提供,它与 GCC-3.x 不兼容)。

FWIW, I can reproduce your issue if I install a GCC-4.8.2 built mingwrt-3.21.1into a GCC-3.4.5 installation. By the same token, I can also successfully use mingwrt-3.21.1with GCC-3.4.5, if I build it with that same version of GCC.

FWIW,如果我安装mingwrt-3.21.1GCC-3.4.5 安装中内置的 GCC-4.8.2,我可以重现您的问题。出于同样的原因mingwrt-3.21.1如果我使用相同版本的 GCC 构建它,我也可以成功地使用GCC-3.4.5 。

Thus, for an authoritative answer: if you mustcontinue to use an obsolete GCC version, youneed to be prepared to rebuild allassociated libraries, using that same obsolete compiler.

因此,对于权威答案:如果您必须继续使用过时的 GCC 版本,需要准备好使用相同的过时编译器重建所有关联的库。

回答by Neil Roy

I had a similar problem, which arose because I foolishly installed a newer version into an old installation. The fix was obvious, I backed up my old installation, then deleted it and done a fresh install. Everything compiled perfectly. Old habits die hard I guess. ;)

我遇到了类似的问题,这是因为我愚蠢地将新版本安装到旧安装中。修复很明显,我备份了旧安装,然后将其删除并重新安装。一切都完美编译。我猜旧习惯很难改掉。;)

回答by StndFish

This might be a "modern" answer to the original question: You may miss the stdc++ and gcc library in MinGW, have to link them by yourself manually.

这可能是原始问题的“现代”答案:您可能会错过 MinGW 中的 stdc++ 和 gcc 库,必须自己手动链接它们。

回答by Juniar

  • To compile a .cpp file you have to issue the following command for it to compile. I suggest you use gcc instead of g++, they both work either way.
  • gcc -c Test.cpp -o Test.exe
  • Or with g++
  • g++ -c Test.cpp -o Test.exe
  • 要编译 .cpp 文件,您必须发出以下命令进行编译。我建议你使用 gcc 而不是 g++,它们都可以工作。
  • gcc -c Test.cpp -o Test.exe
  • 或者用 g++
  • g++ -c Test.cpp -o Test.exe