奇怪的 C++/CLI 連結問題 - 未解析的外部符號,但連結了標頭檔
我試圖在 C++/CLI 中做一些非常簡單的事情,但是我遇到了 Visual Studio 的問題,我不知道為什麼。
我有兩個項目,一個包含一個名為 JNIUtils.h 的標頭檔。它的內容非常簡單——
#pragma once class JNIUtils { public: JNIUtils( void ); ~JNIUtils( void ); };它的實現也很簡單——
#include "stdafx.h" #include "JNIUtils.h" JNIUtils::JNIUtils( void ) { } JNIUtils::~JNIUtils( void ) { }我要做的就是在另一個項目中創建這個新定義類型的對象。在我包含此標頭檔的項目中,我去了
項目屬性 > 配置屬性 > VC++ 目錄 > 包含目錄
我添加了一個條目,其中包含我包含的標頭檔所在的路徑(JNIUtils.h)
我試圖在其中創建此對象的實例的 CPP 文件如下所示:
#include "stdafx.h" #include "JNIUtils.h" using namespace System; int main(array<System::String ^> ^args) { JNIUtils *util = new JNIUtils(); Console::WriteLine(L"Hello World"); return 0; }當我嘗試編譯時,出現以下錯誤:
Error 3 error LNK1120: 2 unresolved externals C:\zcarter\UDK\WebSvcClients\Debug\JNITester.exe JNITester Error 2 error LNK2019: unresolved external symbol "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z) C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj JNITester Error 1 error LNK2028: unresolved token (0A000009) "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z) C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj JNITester誰能告訴我我在這裡做錯了什麼?
當您想將一個項目中的類用於另一個項目時,您需要做兩件事:
1) 將庫編譯程式碼 (.lib) 連結到您的主應用程式碼。 如果您有vs2010,只需在
Project->Properties->Common->References版塊中添加項目,所有的辛苦工作都會為您完成。如果你有 vs2008 或更低版本,則必須將項目添加到
Project->Dependencies.... 然後,進入Project->Properties->Linker->Input並將 .lib 文件添加到現有的 .dll 列表中。使用相對路徑,以便這將在其他電腦上執行。請注意,vs2008 中也有一個Reference部分,但我相信它只適用於 CLR 程序集。2)將標題添加到包含目錄
您已經想到了該部分,但我建議您將它們添加到其中
Project->Properties->C++->Additional Include Directories,因為除非它們與您進行相同的配置,否則您使用的方法將無法在另一台電腦上使用。