[英]When using fstream in a library I get linker errors in the executable
当我添加
#include <fstream>
并尝试使用
std::ifstream (i.e. std::ifstream ifile(pDest))
在我的库中,编译使用该库的项目时出现以下链接器错误:
Error 2 error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function "public: wchar_t * & __thiscall std::vector<wchar_t *,class std::allocator<wchar_t *> >::operator[](unsigned int)" (??A?$vector@PA_WV?$allocator@PA_W@std@@@std@@QAEAAPA_WI@Z) C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\Console.lib(ZipLib.obj) TestingZipper
Error 3 error LNK2001: unresolved external symbol __CrtDbgReportW C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\libcpmtd.lib(stdthrow.obj) TestingZipper
Error 4 error LNK2019: unresolved external symbol __free_dbg referenced in function "private: void __thiscall std::_Yarn<char>::_Tidy(void)" (?_Tidy@?$_Yarn@D@std@@AAEXXZ) C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\Console.lib(ZipLib.obj) TestingZipper
Error 5 error LNK2001: unresolved external symbol __free_dbg C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\libcpmtd.lib(xdebug.obj) TestingZipper
Error 6 error LNK2001: unresolved external symbol __free_dbg C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\libcpmtd.lib(locale0.obj) TestingZipper
Error 7 error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z) C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\libcpmtd.lib(xdebug.obj) TestingZipper
Error 8 error LNK2001: unresolved external symbol __malloc_dbg C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\libcpmtd.lib(locale0.obj) TestingZipper
Error 9 error LNK2019: unresolved external symbol __calloc_dbg referenced in function __Getctype C:\zipprojnotworking\CPP\7zip\UI\TestingZipper\libcpmtd.lib(_tolower.obj) TestingZipper Error 10 error LNK1120: 4 unresolved externals C:\zipprojnotworking\CPP\7zip\UI\Console\Debug\TestingZipper.exe TestingZipper
任何想法为什么?
5 年后......问题(也许还有许多其他问题)已经解决(并被遗忘了:))
您有 1 个包含上述代码的lib项目:我假设它在.c(xx)文件中而不是在.h文件中(包括在两个项目中),以及一个使用前一个的应用程序项目。
我已经考虑过会产生这种行为的配置是什么(构建良好的lib项目和具有这些未解析的外部对象的应用程序项目),唯一站起来的配置是: lib项目不正确。 详细说一下:
这更好地我们可以简化重现行为所需的环境。 您可以切换Project Properties -> Configuration Properties -> General -> Configuration Type并从Static Library (.lib)更改为Dynamic Library (.dll) 。 现在,最后它将链接并且在构建lib项目时将无法吐出错误。
1检查[SO]: Errors when links to protobuf 3 on MSVC 2013有关 CRT 链接类型的详细信息(也检查链接)。 另请查看[SO]:CLR Windows 窗体中的 LNK2005 错误,以获取有关构建C和C++代码时发生的情况的更多详细信息。
关于[MSDN.Blogs] 的几句话:调试与发布构建:在调试模式下构建时,一些检测代码会悄悄地添加到您的代码中以方便调试。 这就是为什么调试构建工件(与它们的发布版本相比):
代码添加通常通过构建步骤(简化版)之间的差异来实现:
最重要的是编译(以及间接的预处理)和链接阶段必须同步。 您的lib项目不是这种情况,因此您的UCRT不匹配(如第三条评论所述)。 要解决此问题,请执行以下任一操作:
我列出了它们,因为我不知道哪个不正确(因为您希望配置设置与配置名称( Debug / Release )相匹配),但我有一种感觉是后者。
此外,请确保在应该协同工作的项目之间具有一致的设置。
对于像我这样认为这个答案没有帮助的人,第二个选项是去:项目属性->配置属性->常规->项目默认值->.NET目标框架版本并将其设置为v4.0
https://connect.microsoft.com/VisualStudio/feedbackdetail/view/806238/unwarranted-linker-errors-using-stl-filestream-class-in-managed-classes-in-c-11-cli有一个模糊的答案来自微软团队解决了我的问题。
我还将此答案添加到同一问题的另一个版本中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.