从第 3 方库中导出 cpp 损坏的函数 [英] Exporting cpp mangled functions from a 3rd party library

查看:25
本文介绍了从第 3 方库中导出 cpp 损坏的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方库作为源代码.它带有一个 Visual Studio 项目,可以从中构建一个 .lib.

I have a third party library as source code. It comes with a Visual Studio project that builds a .lib from it.

不过,我想从 C# 访问该功能,因此我复制了该项目并将其更改为创建了一个 dll.

I want to access the functionality from C# though, so I copied the project and changed it to create a dll.

尽管 DLL 不包含任何导出函数,因此我还创建了一个模块定义 (.def) 文件,并在 EXPORTS 部分添加了我需要的函数.

The DLL didn't contain any exported functions though, so I also created a module definition (.def) file, and added the functions I need in the EXPORTS section.

这适用于在 extern "C" 块中声明的所有函数.但是,我需要的一些函数是在这些块之外声明的.

This works for all the functions that are declared in extern "C" blocks. However, some of the functions that I need are declared outside of those blocks.

如果我将它们添加到 EXPORTS 部分,我会收到错误 LNK2001 unresolved external symbol XYZ :(

If I add those to the EXPORTS section I get the error LNK2001 unresolved external symbol XYZ :(

如果可以避免,我不想更改第 3 方库的源代码.

I don't want to change the sourcecode of the 3rd party library if I can avoid it.

访问这些功能的最优雅、最简单的方法是什么?

What's the most elegant and hopefully simplest way to access those functions as well?

编辑

还有一点需要澄清:据我所知,我想公开的接口中不涉及 C++ 功能.老实说,我不明白为什么 3rd 方作者不只是将少数剩余的函数包含到 extern "C" 块中.这些函数位于头文件的底部,也许添加它们的人犯了一个错误并将它们放在 extern "C" 块范围之外.

One more point for clarification: As far I can tell there is no C++ functionality involved in the interface I want to expose. I honestly don't understand why the 3rd party authors did not just include the few remaining functions into the extern "C" blocks. These functions are at the bottom of the header file, maybe the person that added them just made a mistake and put them outside of the extern "C" blocks scope.

推荐答案

对于 C++,一种方式(恕我直言,最优雅的)是使用 C++/CLI,它是为此而设计的.特别是如果您不仅有函数而且还有类.

For C++ one way (IMHO the most elegant) would be using C++/CLI, which is designed for that. Especially if you have not only functions but also classes.

您创建了一个相当简单的薄包装层:

You create a thin wrapper layer which is fairly simple:

  1. 创建 CLI 类
  2. 放入原类的成员实例
  3. 包装原始类中的所有公共方法

像这样(未经测试):

C++ 原生:

// original c++ class
class Foo {
public:
    Foo(int v) : m_value(v) {}
    ~Foo() {}

    int  value()      { return m_value; }
    void value(int v) { m_value = v; }

private:
    int m_value;
};

CLI 包装器:

// C++/CLI wrapper
ref class FooWrap
{
public:
    FooWrap(int v) { m_instance = new Foo(v); }
    !FooWrap()     { delete m_instance; }
    ~FooWrap()     { this->!FooWrap(); }

    property int value {
        int  get()      { return m_instance->value(); }
        void set(int v) { m_instance->value(v); }
    }

private:
    Foo *m_instance;
};

此处 您可以找到一个简短的操作方法,其中对其进行了更详细的描述.

Here you can find a short howto, which describes it in more detail.

来自您的评论:

[...] 不过我从来没有使用过 C++/CLI,而且这种语言看起来有点混乱.由于项目截止日期很近,我不确定是否有足够的时间来学习它.不过我一定会记住这个选项!

[...] I never worked with C++/CLI though and the language looks a little confusing. Since the project deadline is pretty close I'm not sure if there's enough time to learn it. I'll definitely keep this option in mind though!

如果您不是在寻找最优雅的方式,就像在您的原始问题中一样,而是寻找最快/最短的方式:对于 C++ 一种方式(恕我直言,最短的方式)将使用 C++/CLI,这是为此而设计的.特别是如果你不仅有函数而且还有类.您创建了一个相当简单的薄包装层... 此处,您可以找到一个简短的(10 分钟内)howto,其中更详细地描述了它.

If you are not looking for the most elegant way, as in your original question, but for the fastest/shortest way: For C++ one way (IMHO the shortest way) would be using C++/CLI, which is designed for that. Especially if you have not only functions but also classes. You create a thin wrapper layer which is fairly simple... Here you can find a short (in 10 min) howto, which describes it in more detail.

这篇关于从第 3 方库中导出 cpp 损坏的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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