将 C++/CLI 转换为 C++ 时的陷阱

发布于 2024-08-26 10:35:27 字数 171 浏览 3 评论 0原文

我有一个用 C++/CLI 编写的库,我想打开它。我希望它尽可能跨平台,并且能够编写绑定以供其他语言使用(Java、Python 等)。为此,该库需要采用纯 C++ 语言,以获得最大的灵活性。我认为逻辑结构已经存在,我只需要将它使用的 .NET 库替换为标准 C++ 库即可。这是一个错误的观念吗?进行这种转变时我应该注意什么?

I have a library written in C++/CLI and I want to open it up. I want it to be as cross-platform as possible and be able to write bindings to it for other languages to use (Java, Python, etc, etc). To do this, the library needs to be in plain C++ for maximum flexibility. I figure that the logical structures are already there, I just need to replace the .NET libraries it uses with the standard C++ ones. Is this a misguided notion? What should I watch out for when making this transition?

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

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

发布评论

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

评论(2

誰ツ都不明白 2024-09-02 10:35:28

从未做过 C++/Cli 到 C++ 的移植,但我想到的是:

  • 确保没有内存泄漏。如果可能的话,使用智能指针而不是 gcnew(如果没有,请确保您的代码是异常安全的)。
  • 确保您的库接口仅包含内置类型(内置类型不包括 STL 的类型!但是,如果您采用开源,则这不是强制必要的)

Never done a port of C++/Cli to C++, but this comes to my mind:

  • Make sure that you dont have memory leaks. Use smart-pointers instead of gcnew, if possible (if not, make sure your code is exception safe nontheless).
  • Make sure your libraries interface only consists of builtin types (builtin does not include types of the STL! however this is not coercively necessary if you go open source)
赴月观长安 2024-09-02 10:35:27

这可能比它的价值更麻烦。您可能会遇到以下情况:

  • C++ 中没有垃圾回收。这是最重要的。这可能需要对您的库进行重大重新设计才能进行转换。如果您至少使用 C++ tr1 或 boost 库,则可以通过使用shared_ptr 来实现这一目标,但存在重要的根本区别。例如,您必须警惕循环依赖。此外,如果调试器中没有对它们的特定支持,它们也会使调试变得困难。
  • .Net 类中的函数在 C++ stl 或标准库中没有等效项。最大的麻烦可能是您编写的任何字符串操作代码,因为其中存在很多差异。
  • 类库/程序集不是 C++ 内置的 - 每个平台都有自己的创建动态或共享库的方法,并且对 C++ 共享库的支持不多 - 在许多情况下只有 C 库。准备好将所有内容都变成静态库。
  • 您必须自己管理所有资源。

It might be more trouble than it's worth. Here is what you might come across:

  • There is no garbage collection in C++. This is the big one. This may require a significant redesign of your library just to convert. If you are using at least C++ tr1, or the boost library, you can sort of get there by using shared_ptr, but there are important fundamental differences. For example, you must be wary of circular dependencies. Also, they make debugging difficult without specific support for them in the debugger.
  • Functions in .Net classes which have no equivalent in C++ stl or the standard library. Probably the biggest hurtle will be any string manipulation code you have written since there are lot of differences there.
  • Class libraries/assemblies are not built-in to C++ - every platform has its own method of creating dynamic or shared libraries, and there isn't much support for C++ shared libraries - only C libraries in many cases. Be prepared to make everything a static library.
  • You must manage all your resources yourself.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文