[英]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.