2008-08-22 60 views
1

我正在構建針對一些傳統的第三方庫的應用程序,並且存在鏈接階段的問題。我試圖用Visual Studio 9進行編譯。我的編譯命令是:C++鏈接器無法解析的外部符號

cl -DNT40 -DPOMDLL -DCRTAPI1=_cdecl 
-DCRTAPI2=cdecl -D_WIN32 -DWIN32 -DWIN32_LEAN_AND_MEAN -DWNT -DBYPASS_FLEX -D_INTEL=1 -DIPLIB=none -I. -I"D:\src\include" -I"C:\Program Files\Microsoft Visual Studio 
9.0\VC\include" -c -nologo -EHsc -W1 -Ox -Oy- -MD mymain.c 

代碼乾淨地編譯。鏈接命令是:

​​

是我得到的錯誤是:

app_main.obj : error LNK2019: 
unresolved external symbol 
"_\_declspec(dllimport) public: void 
__thiscall std::locale::facet::_Register(void)" 
([email protected]@[email protected]@@QAEXXZ) 
referenced in function "class 
std::ctype<char> const & __cdecl 
std::use_facet<class std::ctype<char> 
(class std::locale const &)" ([email protected][email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@Z) 

app_main.obj : error LNK2019: 
unresolved external symbol 
"__declspec(dllimport) public: static 
unsigned int __cdecl 
std::ctype<char>::_Getcat(class 
std::locale::facet const * *)" 
([email protected][email protected]@[email protected]@[email protected]@[email protected]@Z) 
referenced in function "class 
std::ctype<char> const & __cdecl 
std::use_facet<class std::ctype<char> 
(class std::locale const &)" ([email protected][email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@Z) 

app_main.obj : error LNK2019: 
unresolved external symbol 
"__declspec(dllimport) public: static 
unsigned int __cdecl 
std::ctype<unsigned 
short>::_Getcat(class 
std::locale::facet const * *)" 
([email protected][email protected]@[email protected]@[email protected]@[email protected]@Z) 
referenced in function "class 
std::ctype<unsigned short> const & 
__cdecl std::use_facet<class std::ctype<unsigned short> >(class 
std::locale const &)" 
([email protected][email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@Z) 

mymain.exe : fatal error LNK1120: 3 
unresolved externals 

注意,這些錯誤是從舊的代碼來了,不是我的代碼 - app_main.obj是部分遺留代碼,而mymain.c是我的源代碼。我已經做了一些搜索,並且我讀過的內容說這種類型的錯誤是由於我的代碼和我鏈接到的庫之間的-MD開關不匹配造成的。由於我正在處理遺留代碼,所以解決方案必須來自我的環境。自從我完成C++工作已經很長時間了,甚至更長的時間,因爲我已經使用了Visual Studio,所以我希望這只是我的一些無知。任何想法如何解決這些問題?

回答

-2

在試圖在VS 2008下編譯這些東西之後,我嘗試了VS-2005的早期版本,並且在警告中工作,而2003只是工作。我仔細檢查了鏈接,並且找不到任何問題,所以要麼我找不到它,要麼這不是問題。

因此要重申,降級到VS 2003將其固定。

5

這些是標準庫引用。確保所有庫(包括標準庫)都使用相同的鏈接。例如。動態鏈接標準庫時不能靜態鏈接。使用的線程模型也是如此。請特別注意,您和第三方庫使用相同的鏈接選項。

這可能是* ss中的一個真正的痛苦。

2

入住這對MSDN

  • /MD使你的應用程序使用的運行時庫的multithread-和具體的DLL版本。
  • /MT使您的應用程序使用運行時庫的多線程靜態版本。

注:「......使鏈接器將使用LIBCMT.LIB來解決外部符號」

所以你需要一個不同的組庫。

如何我去約找出哪些庫鏈接:

  1. 查找構造確實鏈接,並添加/ verbose選項。
  2. 將輸出輸出到文本文件。
  3. 試試不是鏈接的配置。
  4. 查看步驟2的詳細輸出中未解析的符號(在您的情況下爲「_declspec(dllimport)public:void thiscall std :: locale :: facet :: Register(void)」)並查找使用的庫。
  5. 將這些庫添加到要鏈接到的庫的列表中。

舊的skool,但它worked對我來說。

2

如果您仍然希望獲得項目使用VS2008(或將來)我可以使用二進制編輯器來查看問題mainapp.obj目標文件建議編譯。

這是一個來自我的一個小項目的例子。

的zdbException.obj包含以下摘錄

DEFAULTLIB:"libc 
pmtd" /DEFAULTLI 
B:"uuid.lib" /DE 
FAULTLIB:"uuid.l 
ib" /include:?id 
@[email protected]?$o 
streambuf_iterat 
[email protected]?$char_trai 
[email protected]@[email protected]@@[email protected]@ 
@[email protected]@[email protected] 
[email protected] /include:?id 
@[email protected]@st 
[email protected]@[email protected]@A 
/DEFAULTLIB:"LI 
BCMTD" /DEFAULTL 
IB:"OLDNAMES" /E 
DITANDCONTINUE 

注入口/DEFAULTLIB: 「LIBCMTD」。這表明目標文件是使用靜態c運行時多線程調試編譯的。

也有可能在obj中引用的函數在VS2008附帶的標準運行時庫中不推薦使用。