将非托管C ++ DLL与托管C ++类库DLL链接 [英] Linking unmanaged C++ DLL with managed C++ class library DLL

查看:126
本文介绍了将非托管C ++ DLL与托管C ++类库DLL链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如问题创建简单的c ++.net包装器一样.逐步

我正打算在.NET中使用C ++类 但是我在Visual Studio(2008)中构建时遇到问题.

I am tring to use C++ classes in .NET but I am having problems building in Visual Studio (2008).

我有一个非托管类A(用/clr编译的C ++). 我创建了一个C ++/clr类'Class1',该类包装了A并匹配 方法委托给A的方法.

I have an unmanaged class A (C++ compiled with /clr). I created a C++/clr class 'Class1' which wraps A and with matching method delegates to A's methods.

如果我在类库中包含A类的单位源文件 Class1的项目(托管),我没有问题 一切都可以正常运行, 但是我有许多非托管的C ++类(例如A),我正打算将它们放入 DLL,并将该DLL链接到(类包装器的)托管库. [实际上,我目前不需要将这些DLL链接在一起, 但是编译器似乎需要它,并给出了如下所示的错误.]

If I include class A's unit source file in the class library project for Class1 (managed) I have no problems everything links and works fine, But I have many unmanaged C++ classes like A and I am tring to put them in a DLL and link that DLL to the managed library (of class wrappers). [I actually don't see a need to link these DLL's together at this point, but the compiler appears to be requiring it, giving the same errors shown below.]

我创建了VisualC ++/CLR/类库 并添加了我的C ++类(下面列出了A)并进行构建. [我使用默认设置,但 在项目链接器设置中,我都尝试过 用是"和否"注册输出. 没有错误,并且.DLL文件已创建.

I created VisualC++ / CLR / Class library and added my C++ class (A listed below) and build. [I used the default settings but in the project linker settings I've tried both Register output with yes and no.] There were no errors and the .DLL file was created.

我创建了VisualC ++/CLR/类库 并创建了包装类"Class1" 我使用了所有默认设置. 在项目属性下,我单击参考"添加新参考" 并选择了第一步中创建的DLL.

I created VisualC++ / CLR / Class library and created the wrapper class 'Class1' I used all default settings. Under project properties I clicked 'References' 'Add New Reference" and selected the DLL created in the first step.

我收到链接器错误:

test_NET_library.obj : error LNK2028: unresolved token (0A000009) "public: int __thiscall Z::A::m1(int,int)" (?m1@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m1(int,int)" (?m1@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
test_NET_library.obj : error LNK2028: unresolved token (0A00000A) "public: int __thiscall Z::A::m2(int,int)" (?m2@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m2(int,int)" (?m2@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
test_NET_library.obj : error LNK2019: unresolved external symbol "public: int __thiscall Z::A::m1(int,int)" (?m1@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m1(int,int)" (?m1@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
test_NET_library.obj : error LNK2019: unresolved external symbol "public: int __thiscall Z::A::m2(int,int)" (?m2@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m2(int,int)" (?m2@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
C:\temp\test_Cpp_CLI\test_NET_library\Debug\test_NET_library.dll : fatal error LNK1120: 4 unresolved externals

与我在包装器类库项目(有效的选项)中删除A.cpp相同的错误. 我不明白为什么该版本首先尝试解决外部问题 因为这是一个库,而不是一个程序.

The same errors as if I remove A.cpp in the wrapper class library project (the option that works). I don't understand why the build is trying to resolve externals in the first place because this is a library, not a program.

还有其他需要添加到包装类库项目属性中的内容吗? 或注册非托管类的DLL或编译器选项? 我还需要一个.lib文件与DLL一起使用吗? (没有lib文件出现在项目目标目录中)

Is there something else I need to add to the wrapper class library project properties or register the DLL of unmanaged classes, or compiler options? Do I also need a .lib file to go with the DLL? (no lib file appears in the projects target directory)

我是否还必须使用__declspec(dllexport)[它认为这仅适用于C样式函数 不是班级成员.] 就像在问题中一样:从Visual C ++ DLL导出非托管类吗? 即使非托管C ++库是在启用CLR的情况下编译的.

Do I still have to use __declspec(dllexport) [it thought that was only for C style functions not class members.] as in the question: Export Unmanaged Classes from a Visual C++ DLL? even though the unmanaged C++ library is compiled with CLR enabled.

(我也曾尝试将其编译为静态库,但我不知道该如何添加 .lib文件添加到CLR类库项目.

(I did also try compiling as a static library, but I can't figure out how to add the .lib file to the CLR class library project).

我的考试班是

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混合模式DLL创建 我也尝试过:

As per the question: C++/CLI Mixed Mode DLL Creation I have also tried:

#pragma managed(push, off) 
#include "../A.h"
#pragma managed(pop)

这种推动也围绕A.cpp进行.

And also this pushing managed around A.cpp.

更新: 根据mcdave的响应,我删除了产生一个DLL的/clr,现在如何使该DLL可用于我的test_NET_library?

Update: As per mcdave's response I removed the /clr this produced a DLL, now how do I make this DLL available to my test_NET_library?

我尝试了References/Add New Reference,并选择了新的新DLL;并收到消息无法添加对文件'C:.. \ unmanaged_lib.dll'的引用,因为它既不是.NET程序集也不是注册的ActiveX控件.". DLL已添加到项目的文件列表中,但编译器似乎忽略了它.

I tried References/Add New Reference, and selected the new this new DLL; and got the message "Could not add reference to file 'C:..\unmanaged_lib.dll' because it is neither .NET assembly or registered ActiveX control.". The DLL was added to the project's file list, but the compiler appears to be ignoring it.

我尝试了添加/现有项并选择了新的DLL. 但是.DLL文件不是可选文件类型.

I tried Add/Existing item and selected the new DLL. but .DLL files are not a selectable file type.

推荐答案

从您的更新中获得一些提示,我将尝试两个猜测...

With the few hints from your update I will try two guesses...

  1. 当unmanaged_lib是静态链接的lib时,是否已将unmanaged_lib项目设置为test_NET_library的依赖项? (在Project Explorer窗口中,右键单击test_NET_library,选择"Project Dependencies ...",然后选择unmanaged_lib.)
  2. 当unmanaged_lib是DLL时,您需要通过遵循
  1. When unmanaged_lib is a statically linked lib, have you set the unmanaged_lib project to be a dependency of test_NET_library? (In the Project Explorer window, right-click on test_NET_library, select "Project Dependencies..." and select unmanaged_lib.)
  2. When unmanaged_lib is a DLL, you need to export the class from the DLL by following this answer and also make the test_NET_library depend on the unmanaged_lib project.

这篇关于将非托管C ++ DLL与托管C ++类库DLL链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆