如何将 C++ 源文件和头文件添加到 C# .Net 项目? [英] How can I add C++ Source Files and Headers to a C# .Net project?

查看:47
本文介绍了如何将 C++ 源文件和头文件添加到 C# .Net 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 HIDAPI 库用于自制USB 设备.与其将 VC++ 项目编译成它自己的 .dll 并在我拥有的 .Net 项目中引用它,我更愿意将代码存在于 .Net 项目中并能够直接引用它.

I want to make use of the HIDAPI library for a home-brewed USB device. Rather than having to compile the VC++ project into its own .dll and reference it in the .Net project I have, I would prefer to just have the code present within the .Net project and be able to reference it directly.

原因是我想避免让 .dll 引用另一个 .dll,而是只有一个 .dll 文件.

The reason is I want to avoid having a .dll referencing another .dll, and instead just have a single .dll file.

相反,我在做一些 JNI 东西时在 C++ 程序中使用了 C# 类.

I've done it in reverse, sort of, where I have used a C# class within a C++ program when I was doing some JNI stuff.

我在这里尝试做的事情可能吗?是否有在某处的 .Net 项目中使用 C++ 的白痴指南?

Is what I am trying to do here possible? Is there an idiots guide to using C++ in .Net projects somewhere?

推荐答案

等等,有一个肯定的答案,但显然它是完全邪恶的:

Wait, there is a yes answer, but obviously it's completely evil:

如何将 C# 和 C++ 代码混合在一个单个程序集?

如果您的 C++ 代码不是用/clr:safe 编译的(即它是用/clr 或/clr:pure 编译的),请执行以下操作:

If your C++ code is not compiled with /clr:safe (i.e. it is compiled with /clr or /clr:pure), do the following:

1) 将您的 C++ 代码编译成 .obj 文件

1) compile your C++ code into .obj files

2) 将 C# 代码编译为 .netmodule,使用/AddModule 引用 C++ .obj 文件

2) compile your C# code into a .netmodule, using /AddModule to reference the C++ .obj files

3) 使用 C++ 链接器将 C# netmodule 直接与 C++ 目标文件链接以创建混合语言程序集

3) link the C# netmodule directly with the C++ object files using the C++ linker to create a mixed language assembly

还有这个:将本机 C++ 链接到 C# 应用程序

是的,不要那样做.

这篇关于如何将 C++ 源文件和头文件添加到 C# .Net 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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