如何移植C++在 Visual Studio 中编写 C++/CLI 代码?
我有一个用本机 C++ 编写的应用程序,我想在 .NET 虚拟机上运行它。我正在考虑使用 Visual Studio 2008 编译器将 C++ 代码重新编译为 C++/CLI。遗憾的是,我没有找到任何有关如何执行此操作的文档,因此我的问题是:
- 这实际上有意义吗?我正在尝试不可能的事情吗?
- 在哪里可以找到有关该主题的信息?
I have an application written in native C++ which I'd like to get running on the .NET virtual machine. I was thinking of recompiling the C++ code as C++/CLI, using the Visual Studio 2008 compiler. Regrettably, I don't find any documentation on how to do this, so hence my questions:
- Does this actually make sense? Am I trying the impossible?
- Where can information on the topic be found?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
许多本机 C++ 代码实际上只会在 C++/CLI 上编译和运行。这实际上是一种混合编译器,可以调用本机Win32函数并使用OpenGL等标准C库。您甚至可以直接调用 COM 接口(您可以使用本机 C++ 编译器执行的所有操作)。
.Net 库也可用,但您需要创建托管类(使用 ref class 关键字)。您将使用gcnew为这些类分配内存(来自垃圾收集堆)。普通类的内存仍然使用 new 和 delete 分配(来自标准的非垃圾收集堆)。
简而言之,您可以一点一点地迁移到 .Net,尽管在托管类和非托管类之间切换时仍然存在一些摩擦。
我发现这本书很有用:Pro Visual C++/CLI。
A lot of native C++ code will actually just compile and run on C++/CLI. This is really a kind of hybrid compiler that can call native Win32 functions and use standard C libraries like OpenGL. You can even call COM interfaces directly (all the stuff you can do with a native C++ compiler).
The .Net library is also available but for these you create managed classes (using the ref class keyword). You will use gcnew to allocate memory for these classes (from a garbage collected heap). Memory for your normal classes is still allocated using new and delete (from a standard, non garbage-collected heap).
In short, you can migrate to .Net in bits and pieces, though there is still some friction when switching between managed and unmanaged classes.
I found this book useful: Pro Visual C++/CLI.
转到项目属性 ->一般->公共语言运行时支持 ->更改为 /clr
现在称为 CLR。请阅读此处和此处。
Go to project properties -> General -> Common Language Runtime support -> change to /clr
It's called CLR now. Read about it here and here.
在 C++ 中,您只需使用 /clr 重新编译代码库即可。这种技术称为 IJW(It Just Works),因此您可以轻松地将现有的类与 CLR 结合使用。
In C++ you can simply recompile your codebase with /clr. This technique called IJW (It Just Works) so you can easily use your existing classes with CLR.