我使用mingw-w64在windows上部署C++,一切都很正常,只是当我使用库函数时,在运行时会报告以下问题
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(mbrscan.o):(.text+0xb6): undefined reference to `SetLastError@4'
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(wcharmap.o):(.text+0x208): undefined reference to `WideCharToMultiByte@32'
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../libmingwex.a(codeset.o):(.text+0xbe): undefined reference to `GetCPInfo@8'
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../libmingwex.a(codeset.o):(.text+0x143): undefined reference to `GetCPInfo@8'
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/libgcc_eh.a(emutls.o): in function `_gthread_key_create':
/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libgcc/./gthr-default.h:590: undefined reference to `TlsAlloc@0'
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: /home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libgcc/./gthr-default.h:597: undefined reference to `__mingwthr_key_dtor'
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/libgcc_eh.a(emutls.o): in function `_gthread_getspecific':
/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libgcc/./gthr-default.h:621: undefined reference to `SetLastError@4'
d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: d:/mingw-w64/bin/../lib/gcc/mingw32/9.2.0/libgcc_eh.a(emutls.o): in function `_gthread_setspecific':
/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libgcc/./gthr-default.h:629: undefined reference to `TlsSetValue@8'
collect2.exe: error: ld returned 1 exit status
是什么原因造成的? 你能帮我解决吗?
--
一个最小的例子,再现了我认为的问题。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
cout << to_string(42) << endl;
return 0;
}
与MinGW GCC 9.2.0的链接。
C:\Users\dash\repos\link-error>g++ --version
g++ (MinGW.org GCC Build-20200227-1) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
C:\Users\dash\repos\link-error>g++ main.cpp -o main
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(mbrscan.o):(.text+0xb6): undefined reference to `SetLastError@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(wcharmap.o):(.text+0x208): undefined reference to `WideCharToMultiByte@32'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingwex.a(codeset.o):(.text+0xbe): undefined reference to `GetCPInfo@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingwex.a(codeset.o):(.text+0x143): undefined reference to `GetCPInfo@8'
collect2.exe: error: ld returned 1 exit status
当你把你的问题标记为 gcc
错,实际上是来自 ld
,这就是链接器。
你编译一些东西(你没有显示命令),然后链接器抱怨说 SetLastError@4
. 我打赌有一个函数 SetLastError()
(mbrscan.c?),但您没有为您的链接器包含相关的对象代码或库。
请检查您是否遗漏了任何库 (-l
标志),或任何 *.o
文件在你的编译命令中。
有同样的问题,这让我疯狂。但解决方法很简单(感谢):MinGW不能链接,因为库没有安装。
Start->cmd
mingw-get
mingw32-libmingwex
在...之下 MinGW Standard Libraries
mingw32-libmingwex-dll
和 mingw32-libmingwex-dev
Installation->Apply Changes
完成!
PS: 我建议把这个主题改成 "MinGW - linker error "或类似的主题,并加上相应的标签。