簡體   English   中英

在庫中使用 fstream 時,我在可執行文件中收到鏈接器錯誤

[英]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項目不正確。 詳細說一下:

  • 一個缺失的符號是CrtDbgReport ,它告訴我該庫是在調試模式下構建的。
  • lib正在構建OK的事實告訴我:
    1. lib項目沒問題,報錯在app項目
    2. lib項目不好(應用項目可能好也可能不好)但它是一個靜態庫 - 在這種情況下, .obj文件只是在生成的.lib文件中“合並”在一起 - 它沒有鏈接鏈接器此時不會搜索未解析的外部文件,它只會搜索此庫何時鏈接到可執行文件( .exe.dll )中。 我在錯誤日志中看到libcpmtd.lib (1)的事實證明了這一點:使用靜態運行時庫 ((U)CRT)版本(尤其是在包含庫的項目中)僅在構建靜態應用程序時才有意義(通常設計為在剝離的環境中運行 - 例如在“ Windows 簡約核心”發行版上,它只需要核心庫,如: ntdll.dllkernel32.dll ,...)。
  • 在鏈接時(應用程序項目)沒有重復符號這一事實排除了第一個選項。

這更好地我們可以簡化重現行為所需的環境。 您可以切換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 錯誤,以獲取有關構建CC++代碼時發生的情況的更多詳細信息。

關於[MSDN.Blogs] 的幾句話:調試與發布構建:在調試模式下構建時,一些檢測代碼會悄悄地添加到您的代碼中以方便調試。 這就是為什么調試構建工件(它們的發布版本相比):

  • 有明顯更大的尺寸
  • 跑得更慢

代碼添加通常通過構建步驟(簡化版)之間的差異來實現:

  • 預處理:定義了_DEBUG宏(與定義NDEBUG 的Release相反)。 您可以瀏覽標准包含文件,並且您會在這些宏上看到許多預處理器指令(主要是#ifdef )。 這個階段對編譯階段有直接影響
  • 鏈接:選擇了正確的庫(實際上包含該檢測代碼)

最重要的是編譯(以及間接的預處理)和鏈接階段必須同步 您的lib項目不是這種情況,因此您的UCRT不匹配(如第三條評論所述)。 要解決此問題,請執行以下任一操作

  • _DEBUG / NDEBUG宏定義更改為: Project Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
  • 更改UCRT類型:項目屬性 -> C/C++ -> 代碼生成 -> 運行時庫

我列出了它們,因為我不知道哪個不正確(因為您希望配置設置與配置名稱( 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM