使用CLI或任何其他方式将C ++库静态链接到C#进程 [英] Statically linking a C++ library to a C# process using CLI or any other way

查看:62
本文介绍了使用CLI或任何其他方式将C ++库静态链接到C#进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以采用用C ++编写的静态库,并以某种方式将其集成到.NET进程中,从而将.NET进程与lib集成到一个exe文件中?

什么我的意思是说我知道我可以从C#进程中调用c ++ DLL,但用户仍然必须拥有C ++ DLL,但是是否可以使用lib而不是DLL?(这样,用户不会甚至知道该exe使用我的库)。
,如果可能,怎么做?

Is it possible to take a static library written in C++ and somehow integrate it into a .NET process in a way that the .NET process will be integrated with the lib into one exe file?
What I mean is that I know I can invoke a c++ DLL from within a C# process but the user will still have to have the C++ DLL, but is it possible to use a lib instead of a DLL?(this way the user won't even know that the exe uses my library). and if it is possible, how?

推荐答案

在其他托管语言中,C ++独一无二,它允许使用混合模式-a托管( C ++ / CLI )和本机C ++在同一编译中的组合单位(dll / exe / lib),带有来回调用。也许您可以利用它来创建一种胶合层。我从未尝试过。

Unique among other managed languages, C++ allows for mixed mode - a combination of managed (C++/CLI) and native C++ within the same compilation unit (dll/exe/lib), with calls back and forth. Maybe you can leverage that, create a kind of a glue layer. I've never tried though.

关键是/ clr编译器开关-您将其应用于项目中的某些文件,而不应用于其他文件。然后,您可以创建一些受管理的类/函数。非托管位可以看到它们并调用它们,反之亦然。透明传递原始类型是很容易的,对于字符串来说,存在一些编组技巧。我可以在星期一发布更多内容。

The key is the /clr compiler switch - you apply it to some files in the project but not others. Then you create some classes/functions as managed. The unmanaged bits can see them and call them, and vice versa. Passing primitive types around is done transparently, for strings there's some marshaling trickery. I'll be able to post more on Monday.

编辑:似乎需要一些深奥的魔术。不在混合C ++方面-在将结果链接到C#方面。您会发现,Visual Studio IDE并不容易支持该方案。

seems like some deep magic would be required. Not on the mixed C++ side - on the linking the result to C# side. The Visual Studio IDE does not readily support the scenario, you see. Chances are, the regular build process won't be of any use.

EDIT2:您可以通过指定/ LN命令行选项将C ++位编译为.netmodule。到编译器,/ NOASSEMBLY到链接器。现在,将其链接到C#exe ...

you can compile your C++ bits to a .netmodule by specifying a /LN command line option to the compiler and /NOASSEMBLY to a linker. Now, to link that to the C# exe...

这篇关于使用CLI或任何其他方式将C ++库静态链接到C#进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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