visual-studio 人们如何处理警告 C4793:'some_function':函数编译为本机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2784864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:55:48  来源:igfitidea点击:

How do people handle warning C4793: 'some_function' : function compiled as native?

visual-studioc++-cliopencvwarningspragma

提问by Michael Repucci

I'm using the OpenCV library and one of its header files, cxoperations.hpp, generates "warning C4793: 'anonymous namespace'::CV_XADD' : function compiled as native", if my C++ project is compiled with CLR support. I can prevent the warning by surrounding the OpenCV header include like this:

"warning C4793: 'anonymous namespace'::CV_XADD' : function compiled as native"如果我的 C++ 项目是使用 CLR 支持编译的,我正在使用 OpenCV 库及其头文件之一 cxoperations.hpp 会生成。我可以通过包围 OpenCV 标头来防止警告,如下所示:

#pragma managed(push,off)
#include <cv.h>
#pragma managed(pop)

But the project that actually uses OpenCV isn't compiled with CLR support, it's a native C++ static library. The project that does have CLR support, and generates this warning without the pragma statements, simply uses this static library. So I'm a bit surprised that the warning was created at all, especially given the fact that the entire static library is not compiled with CLR support, and yet it's only this one header that causes the problem.

但是实际使用 OpenCV 的项目并没有使用 CLR 支持编译,它是一个原生的 C++ 静态库。具有 CLR 支持并在没有 pragma 语句的情况下生成此警告的项目仅使用此静态库。因此,我对创建警告感到有点惊讶,特别是考虑到整个静态库没有使用 CLR 支持编译,但只有这个头文件导致了问题。

Thus this solution seems sub-optimal to me. Is this how you would handle this warning, or can you recommend a better practice?

因此,这个解决方案对我来说似乎不是最佳的。这是您将如何处理此警告,还是您可以推荐更好的做法?

采纳答案by Blindy

I think what you want is this:

我想你想要的是这个:

#pragma unmanaged
#include <cv.h>
#pragma managed
// managed code wrapping unmanaged opencv functions

A C++/CLI project can contain both managed and unmanaged parts, and the compiler takes care of marshalling data between the 2 for you. The managed entry points will be callable from normal .NET apps (like C# and the rest) and will use garbage collection, and they'll call unmanaged functions to do the heavy lifting.

C++/CLI 项目可以同时包含托管和非托管部分,编译器会为您处理这两者之间的数据编组。托管入口点可从普通 .NET 应用程序(如 C# 和其他应用程序)调用,并将使用垃圾收集,并且它们将调用非托管函数来完成繁重的工作。

回答by fmuecke

I think you should suppress the warning. The MSDNdoc explicitly states that the managed/unmanaged pragmas should not be used before include statements.

我认为你应该抑制警告。在MSDN文档明确规定,托管/非托管编译指示不应该被使用过包括语句。

#pragma warning(push)
#pragma warning(disable: 4793) // methods are compiled as native (clr warning)
#include <cv.h>
#pragma warning(pop)

回答by Christian

If you cannot change existing code files, you can get rid of the warning by disabling CLR support for the specific file that shows warning 4793. Of course, this only works if this file does not make any use of CLR features.

如果您无法更改现有代码文件,您可以通过禁用对显示警告 4793的特定文件的 CLR 支持来消除警告。当然,这仅在此文件不使用任何 CLR 功能时才有效。

To disable CLR support for a specific file, locate it in Solution Explorer, right-click and open its Property Pages. Set Common Language RunTime Supportto No Common Language RunTime Support. Don't forget to do this for All Configurationsand All Platforms.

要禁用对特定文件的 CLR 支持,请在解决方案资源管理器中找到它,右键单击并打开其属性页。将公共语言运行时支持设置为无公共语言运行时支持。不要忘记为所有配置所有平台执行此操作

Turn off CLR support per file

关闭每个文件的 CLR 支持