简体   繁体   English

将C ++ .lib和.h文件导入C#项目吗?

[英]Import a C++ .lib and .h file into a C# project?

I have just started a C# project and want to import a C++ .lib and it's corresponding header (.h) file. 我刚开始一个C#项目,想导入一个C ++ .lib及其对应的头文件(.h)。

I've read various posts that all mention .dll, rather than .lib, which is confusing me. 我读过各种都提到.dll而不是.lib的帖子,这使我感到困惑。

The image below shows the .lib and .h file I'm referring to, all I've done is drag them into the project. 下图显示了我所引用的.lib和.h文件,我所做的只是将它们拖到项目中。

在此处输入图片说明

Can anyone point me to a clearer explanation of how to go about doing this? 谁能指出我要如何做的更清晰的解释? I'm sure it can't be as hard as it seems. 我敢肯定它不会像看起来那样难。

This is, unfortunately, a non-trivial problem. 不幸的是,这是一个不平凡的问题。

The reason is primarily due to the fact that C++ is an unmanaged language . 原因主要是由于C++是一种非托管语言 C# is a managed language . C#是一种托管语言 Managed and unmanaged refers to how a language manages memory. 托管和非托管是指一种语言如何管理内存。

  • C++ you must do your own memory management (allocating and freeing), C++您必须进行自己的内存管理(分配和释放),
  • C# .NET Framework does memory management with a garbage collector . C# .NET Framework使用垃圾回收器进行内存管理。

In your library code 在您的库代码中

You must make sure all of the places you call new , must call delete , and the same goes for malloc and free if you are using the C conventions. 必须确保调用new所有位置,必须调用delete ,并且如果使用C约定,则mallocfree同样适用。

You will have to create a bunch of wrapper classes around your function calls, and make sure you aren't leaking any memory in your C++ code. 您将必须在函数调用周围创建一堆包装器类 ,并确保您没有泄漏C++代码中的任何内存。

The problem 问题

Your main problem (to my knowledge) is you won't be able to call those functions straight in C# because you can't statically link unmanaged code into managed code . (据我所知)您的主要问题是您将无法直接在C#调用这些函数,因为您无法将非托管代码静态链接到托管代码

You will have to write a .dll to wrap all your library functions in C++ . 您将必须编写一个.dll以将所有库函数包装在C++ Once you do, you can use the C# interop functionality to call those functions from the dll. 完成后,您可以使用C#互操作性功能从dll调用这些功能。

[DllImport("your_functions.dll", CharSet = CharSet.Auto)]
public extern void your_function();

What you could do, is creating a C++/CLI wrapper and expose the functionality of the lib you want to use via your wrapper. 您可以做的是创建一个C ++ / CLI包装器,并通过包装器公开要使用的库的功能。 The created wrapper dll you can easily reference in your C# project. 您可以在C#项目中轻松引用创建的包装dll。 This of course takes a little bit of work to create the managed/unmanaged wrapper, but will pay off in the long run. 当然,创建托管/非托管包装器需要花费一些工作,但从长远来看会有所回报。

To create a managed C++ project select under the C++ project templates CLR and Class Library. 要创建托管的C ++项目,请在C ++项目模板CLR和类库下选择。 Here you can link to your lib, use the header file the way you are used to. 在这里,您可以链接到您的库,以惯用的方式使用头文件。

Next create a new class (ref class) and wrap your library in it. 接下来,创建一个新类(ref类)并在其中包装您的库。 An example might look something like this: 一个例子可能看起来像这样:

LibHeader.h

int foo(...);

You write a wrapper class like this: Header: 您编写这样的包装器类:标头:

Wrapper.h

public ref class MyWrapper
{
    public:
        int fooWrapped();
};

Your Implementation: 您的实施:

Wrapper.cpp

#include Libheader.h

int MyWrapper::fooWrapped()
{
     return foo();
}

Namespaces and all the good stuff omitted for simplicity. 为了简单起见,省略了命名空间和所有好的内容。 Now you can use MyWrapper in your C# code just as easy as any other managed class. 现在,您可以像其他任何托管类一样轻松地在C#代码中使用MyWrapper。 Of course when the interface of the lib gets more complicated you have to think about it a bit more, but it might help to separate the lib-code from your application. 当然,当lib的接口变得更复杂时,您必须多考虑一下,但这可能有助于将lib代码与应用程序分开。 Hope to have shed some light on it. 希望对此有所了解。

It is 'as hard as it seems'. 这“看起来很难”。 C++ and C# are ambivalent. C ++和C#是矛盾的。 The first has deterministic destruction, the second not. 第一个具有确定性破坏,第二个则没有。 Now, you write C++/cli delaying the destruction to some finalizer called by the garbage collector working in it's own thread, introducing problems all over the place (thread safety, are C++ members (used in c++/cli) valid?, ...). 现在,您编写C ++ / cli来将销毁延迟推迟到由在其自己的线程中工作的垃圾收集器调用的终结器,从而在各处引入问题(线程安全,C ++成员(用于c ++ / cli)是否有效?,... )。 Even worse the GC might suggest C++ objects being tiny (a pointer) and provoke a kind of memory leak (due to late deallocating tiny objects). 更糟糕的是,GC可能会建议C ++对象很小(一个指针),并且会引起某种内存泄漏(由于后期重新分配了很小的对象)。 Essentially you end up in writing a GC on top of the C++/cli GC to delete non C++/cli (!) objects in the main thread or another. 本质上,您最终要在C ++ / cli GC之上编写GC,以删除主线程或其他线程中的非C ++ / cli(!)对象。 All that is insane, ... 一切都疯了,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM