2010-10-23 71 views
8

非托管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文件不是可选择的文件类型。

+0

尝试从类A的DLL中删除/ clr(意思是使它成为'普通的'win32 dll),如果这不起作用更新你的问题。 – mcdave 2010-10-30 14:08:11

+0

@mcdave:我这样做了,但后来我不知道如何让test_NET_libary'知道'或'链接到'这个包含A的unmanaged_lib.DLL(请参阅我上面的编辑)。这是我试图使用unmanaged_lib作为一个静态链接的.lib相同的问题。 – 2010-11-06 04:05:16

回答

6

与你更新一些提示,我将要做两个猜测...

  1. 当unmanaged_lib是一个静态链接的lib,你设置unmanaged_lib项目是test_NET_library的依赖? (在项目资源管理器窗口中,右键单击test_NET_library,选择“Project Dependencies ...”并选择unmanaged_lib。)
  2. 当unmanaged_lib是DLL时,您需要通过以下this答案导出DLL中的类,还需要使test_NET_library依赖于unmanaged_lib项目。
+0

解决方案1工作(通常我使用Embarcadero C++ Builder,您只需将编译.lib文件添加到您的项目中,甚至不需要在项目组(解决方案)中包含所有静态库项目,也无需设置依赖关系)。 – 2010-11-08 22:35:24