我将如何使用 C++/CLI dll 将本机代码与 C# 中的多个依赖库包装在一起?

发布于 2024-09-10 16:16:24 字数 288 浏览 8 评论 0原文

我想知道如何正确设置一个 C++/CLI 库来包装具有多个依赖项的本机 C++ 代码。我尝试过静态和动态地将本机库链接到其依赖库,但没有成功。

托管 C++/CLI dll 构建得很好,可以添加为 C# 项目的引用。但是,当我尝试使用任何定义的类时,我会收到 BadImageFormatException 或 FileNotFoundException ,具体取决于我的链接方式。我相信我需要在 CLI 库中指定依赖库,以便将其加载到清单中,但我不确定该过程。另外,因为我知道它会出现,所以我已经验证了所有涉及的库都是基于 x86 架构构建的。

I am wondering how I would go about correctly setting up a C++/CLI library that wraps native c++ code that has several dependencies. I have tried both statically and dynamically linking the native library to its dependent libraries with no luck.

The Managed C++/CLI dll builds just fine and can be added as a reference to a C# project. However when I attempt to use any of the defined classes i receive either a BadImageFormatException or FileNotFoundException depending how i linked. I believe I need to specify the dependent libraries in the CLI library so it is loaded in the manifest but I am unsure of the process. Also because i know it will come up, I have verified that all of the libraries involved are built on the x86 architecture.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓦然回首 2024-09-17 16:16:24

我找出了问题所在,现在一切正常。这是几个不正确的事情同时发生的组合。

如果有人有同样的问题,我通过设置以下内容解决了它:

1)引用的Boost库(特别是boost_thread)需要使用BOOST_THREAD_USE_DLL预处理器进行编译(其他boost库可能需要BOOST_ALL_DYN_LINK来动态链接所有内容)。这显然是一个常见问题。

2) 我验证了所有依赖项都在系统路径中(就像 R Ubben 重申的那样)

3) 我使用 DependencyWalker(来自 sourceforge 的depends.exe)来分析我编译的托管 DLL。事实证明,所使用的 libpq.lib 库实际上引用了其他 DLL,这些 DLL 未包含在 lib 文件夹中,而是包含在 bin 文件夹中。所以需要将其添加到路径中。

4)我的包装器的一部分是使用列表的#include 标头。这迫使我的库链接到 2.0 框架相关的库。这与我的 4.0 客户端目标 C# 应用程序不兼容。这只能通过解析编译警告才知道(之前由于 C++ 生成太多警告而被隐藏。我知道这很愚蠢)。然而,这会导致抛出 System.BadImageFormateException,尽管所有内容都针对相同的 x86 架构。

希望能帮助其他有同样问题的人。 BadImageFormateException 和 FileNotFoundException 太模糊且无用。

I figured out the problem and everything is working correctly now. It was a combination of several incorrect things all happening together.

If anyone has the same issue, I resolved it by setting up the following:

1) The Boost libraries that were referenced (specifically boost_thread) needed to be compiled with BOOST_THREAD_USE_DLL preprocessor (other boost libraries may need BOOST_ALL_DYN_LINK to just dynamically link everything). This is apparently a common issue.

2) I verified that all dependencies were in system Path (like R Ubben reiterated)

3) I used the DependencyWalker (depends.exe from sourceforge) to analyze my compiled managed DLL. It turned out that the libpq.lib library being used actually referenced additional DLLs that were not included in the lib folder but in the bin folder. So that need to be added to the Path.

4) Part of my wrapper was using the #include header for lists. This forced my library to link against 2.0 framework dependent libraries. This was not compatible with my 4.0 client targeted C# application. This was only made known by parsing through the Warnings from compiling (previously hidden due to C++ generating too many..foolish i know). However this was resulting in a System.BadImageFormateException being thrown despite everything targeting the same x86 architecture.

Hope that helps anyone else who has the same problem. The BadImageFormateException and FileNotFoundException were entirly too vague and unhelpful.

独闯女儿国 2024-09-17 16:16:24

您应该使 C++ dll 处于发布模式,并使用 extern "C" 来处理公共静态内容。

You should make the C++ dll in release mode and use extern "C" for public static things.

少年亿悲伤 2024-09-17 16:16:24

当依赖库不在路径中时,我收到了该错误。包装器库是因为引用而找到的,但是引用并没有同时照顾依赖库。尝试将它们显式放置在程序执行的位置。

I have gotten that error when the dependent libraries were not in the path. The wrapper library is found because of the reference, but the reference does not also take care of the dependent libraries. Try placing them explicitly where the program is executing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文