当我添加
#include <fstream>
并尝试使用
std::ifstream (i.e. std::ifstream ifile(pDest))
在我的库中,编译使用该库的项目时出现以下链接器错误:
in my library I get the following linker errors when compiling a project whih uses the library:
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@@QAEAAPA_WI@Z) C:zipprojnotworkingCPP7zipUITestingZipperConsole.lib(ZipLib.obj) TestingZipper
Error 3 error LNK2001: unresolved external symbol __CrtDbgReportW C:zipprojnotworkingCPP7zipUITestingZipperlibcpmtd.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@AAEXXZ) C:zipprojnotworkingCPP7zipUITestingZipperConsole.lib(ZipLib.obj) TestingZipper
Error 5 error LNK2001: unresolved external symbol __free_dbg C:zipprojnotworkingCPP7zipUITestingZipperlibcpmtd.lib(xdebug.obj) TestingZipper
Error 6 error LNK2001: unresolved external symbol __free_dbg C:zipprojnotworkingCPP7zipUITestingZipperlibcpmtd.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@PADH@Z) C:zipprojnotworkingCPP7zipUITestingZipperlibcpmtd.lib(xdebug.obj) TestingZipper
Error 8 error LNK2001: unresolved external symbol __malloc_dbg C:zipprojnotworkingCPP7zipUITestingZipperlibcpmtd.lib(locale0.obj) TestingZipper
Error 9 error LNK2019: unresolved external symbol __calloc_dbg referenced in function __Getctype C:zipprojnotworkingCPP7zipUITestingZipperlibcpmtd.lib(_tolower.obj) TestingZipper Error 10 error LNK1120: 4 unresolved externals C:zipprojnotworkingCPP7zipUIConsoleDebugTestingZipper.exe TestingZipper
知道为什么吗?
5 年后...问题(也许还有许多其他问题)已经解决(并且被遗忘了 :) )
5+ years later... the problem (and maybe many others) is already solved (and forgotten :) )
您有 1 个 lib 项目,其中包含上述代码:我假设它在 .c(xx) 文件中,而不是在 .h 文件中em> 文件(包含在两个项目中),以及一个使用前一个的 app 项目.
我已经考虑过会产生这种行为的配置是什么(lib 项目构建良好,app 项目具有这些未解析的外部/em>) 而唯一站得住脚的配置是:lib 项目不正确.让我详细说明:
You have 1 lib project that contains the code above: I assume it's in a .c(xx) file and not in a .h file (included in both projects), and an app project that makes use of the previous one.
I've thought about what could the configuration that would yield this behavior be (the lib project building fine and the app project having these unresolved externals) and the only configuration that stands up is: the lib project is not correct. Let me detail:
这样更好,我们可以简化重现行为所需的环境.您可以切换项目属性 -> 配置属性 -> 常规 -> 配置类型并从 静态库 (.lib) 更改为 动态库 (.dll).现在,在构建 lib 项目时,它最终将链接并且将失败并吐出错误.
This is better that we can simplify the environment required for reproducing the behavior. You can switch Project Properties -> Configuration Properties -> General -> Configuration Type and change from Static Library (.lib) to Dynamic Library (.dll). Now, at the end it will link and will fail spitting the errors when building the lib project.
1 检查 [SO]:在 MSVC 2013 上链接到 protobuf 3 时出错,了解有关 CRT 链接类型的详细信息(也请检查链接).还要检查 [SO]:CLR Windows 窗体中的 LNK2005 错误有关构建 C 和 C++ 代码时会发生什么的更多详细信息.
1 Check [SO]: Errors when linking to protobuf 3 on MSVC 2013 for details about the CRT linking types (check the links as well). Also check [SO]: LNK2005 Error in CLR Windows Form for more details about what happens when building C and C++ code.
关于[MSDN.Blogs]的几句话: Debug vs Release 构建:在调试模式下构建时,一些检测代码会悄悄地添加到您的代码中以方便调试.这就是为什么调试构建工件(vs它们的Release对应物):
A few words about [MSDN.Blogs]: Debug vs Release builds: when building in Debug mode, some instrumentation code is silently added in your code to facilitate debugging. That's why Debug build artifacts (vs their Release counterparts):
代码添加通常是通过构建步骤之间的差异来实现的(简化版):
The code addition is typically achieved by differences between build steps (simplified version):
最重要的是编译(以及间接预处理)和链接阶段必须同步.您的 lib 项目不是这种情况,因此您存在 UCRT 不匹配(如第三条 评论所述).要解决此问题,要么:
The most important thing is that the compile (and indirectly, preprocess) and link phases must be in sync. This is not the case for your lib project, so you have an UCRT mismatch (as the 3rd comment states). To fix this, either:
我列出了它们,因为我不知道哪个不正确(因为您希望配置设置与配置名称匹配(Debug/Release)),但我感觉是后者.
I listed them both, since I don't know which one is incorrect (as you would want the configuration settings to match the configuration name (Debug / Release)), but I have a feeling that it's the latter.
此外,请确保在应该协同工作的项目之间具有一致的设置.
Also, make sure to have consistent settings across projects that are supposed to work together.
这篇关于在库中使用 fstream 时,我在可执行文件中收到链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!