非託管C++ DLL正如我在特林.NET 使用C++類的問題Creating simple c++.net wrapper. Step-by-step鏈接與託管C++類庫DLL
,但我有問題的建築在Visual Studio(2008年)。
我有一個非託管類A(用/ clr編譯的C++)。 我創建了一個C++/clr類'Class1',它包裝了A,並將匹配的方法委託給A的方法。
如果我包括類庫 項目的Class1(管理)類A的單元源文件我沒有問題 一切環節和工作正常, 但是我有很多非託管C++類,如A和我特林放他們在 一個DLL並將該DLL鏈接到託管庫(類包裝器)。 [其實我不認爲有必要在這一點上, 聯繫在一起,這些DLL的,但是編譯器似乎需要它,給下面顯示的同樣的錯誤。]
我創建VISUALC++/CLR /類庫 並添加了我的C++類(下面列出)並構建。 [我使用默認設置,但 在項目鏈接器設置我已經嘗試 寄存器輸出與是和否] 沒有錯誤和.DLL文件被創建。
我創建了VisualC++/CLR /類庫 並創建了包裝類'Class1' 我使用了所有默認設置。 在項目屬性我點擊「引用」「添加新引用」 並選擇在第一步中創建的DLL
我得到的鏈接錯誤:
test_NET_library.obj : error LNK2028: unresolved token (0A000009) "public: int __thiscall Z::A::m1(int,int)" ([email protected]@[email protected]@[email protected]) referenced in function "public: int __clrcall test_NET_library::Class1::m1(int,int)" ([email protected]@[email protected]@[email protected])
test_NET_library.obj : error LNK2028: unresolved token (0A00000A) "public: int __thiscall Z::A::m2(int,int)" ([email protected]@[email protected]@[email protected]) referenced in function "public: int __clrcall test_NET_library::Class1::m2(int,int)" ([email protected]@[email protected]@[email protected])
test_NET_library.obj : error LNK2019: unresolved external symbol "public: int __thiscall Z::A::m1(int,int)" ([email protected]@[email protected]@[email protected]) referenced in function "public: int __clrcall test_NET_library::Class1::m1(int,int)" ([email protected]@[email protected]@[email protected])
test_NET_library.obj : error LNK2019: unresolved external symbol "public: int __thiscall Z::A::m2(int,int)" ([email protected]@[email protected]@[email protected]) referenced in function "public: int __clrcall test_NET_library::Class1::m2(int,int)" ([email protected]@[email protected]@[email protected])
C:\temp\test_Cpp_CLI\test_NET_library\Debug\test_NET_library.dll : fatal error LNK1120: 4 unresolved externals
同樣的錯誤,如果我刪除A. CPP在包裝類庫項目(即工程的選項)。 我不明白爲什麼構建試圖在首位 解決的外部,因爲這是一個庫,而不是一個項目。
有什麼否則我需要添加到包裝呃類庫項目屬性 或註冊非託管類的DLL或編譯器選項? 我是否還需要一個.lib文件才能使用DLL? (沒有LIB文件出現在項目目標目錄)
我仍然必須使用__declspec(dllexport)的[它認爲,只有對C風格的函數 不是類成員。] 如問題:Export Unmanaged Classes from a Visual C++ DLL? 連儘管非託管C++庫是在啓用CLR的情況下編譯的。 (我也嘗試編譯爲靜態庫,但我不知道如何將.lib文件添加到CLR類庫項目中)。
我的測試類是
namespace Z
{
class A
{
public:
int m1(int p1, int p2);
int m2(int p3, int p4);
};
};
與實施:
#include "A.h"
namespace Z
{
int A::m1(int p1, int p2) { return p1+p2; };
int A::m2(int p3, int p4) { return p3 * p4; };
};
和包裝類
#pragma once
#include "../A.h"
using namespace System;
namespace test_NET_library {
public ref class Class1
{
private: Z::A *a;
public: Class1()
: a(new Z::A)
{}
public: inline int m1(int p1, int p2)
{ return a->m1(p1,p2);
};
public: inline int m2(int p3, int p4)
{return a->m2(p3,p4);
};
};
}
按照問題:C++/CLI Mixed Mode DLL Creation 我也曾嘗試:
#pragma managed(push, off)
#include "../A.h"
#pragma managed(pop)
而且這種推動管理A.cpp。
更新: 根據mcdave的迴應我刪除了/ clr這產生了一個DLL,現在如何讓這個DLL可用於我的test_NET_library?
我試過References/Add New Reference,並選擇了新的這個新的DLL;並得到消息「無法添加引用文件'C:.. \ unmanaged_lib.dll',因爲它不是.NET程序集或註冊的ActiveX控件。」該DLL被添加到項目的文件列表中,但編譯器似乎忽略了它。
我嘗試添加/現有項目,並選擇新的DLL。 但.DLL文件不是可選擇的文件類型。
嘗試從類A的DLL中刪除/ clr(意思是使它成爲'普通的'win32 dll),如果這不起作用更新你的問題。 – mcdave 2010-10-30 14:08:11
@mcdave:我這樣做了,但後來我不知道如何讓test_NET_libary'知道'或'鏈接到'這個包含A的unmanaged_lib.DLL(請參閱我上面的編輯)。這是我試圖使用unmanaged_lib作爲一個靜態鏈接的.lib相同的問題。 – 2010-11-06 04:05:16