C++ 如何修复这个 vs10 不一致的 dll 链接警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5341661/
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-08-28 17:56:28  来源:igfitidea点击:

How can I fix this vs10 inconsistent dll linkage warning?

c++visual-studiowarnings

提问by mmr

I have a series of warnings that I'm trying to fix when building gdcmusing visual studio 10 (32 bit version):

在使用 Visual Studio 10(32 位版本)构建gdcm时,我尝试修复一系列警告:

4>..\..\..\..\gdcm\Utilities\gdcmexpat\lib\xmlparse.c(647): warning C4273: 'XML_ParserCreate' : inconsistent dll linkage
4>          d:\src\gdcm\gdcm\utilities\gdcmexpat\lib\expat.h(206) : see previous definition of 'XML_ParserCreate'

The function call itself looks like:

函数调用本身看起来像:

XML_Parser XMLCALL
XML_ParserCreate(const XML_Char *encodingName)
{
   return XML_ParserCreate_MM(encodingName, NULL, NULL);
}

where

在哪里

#define XMLCALL __cdecl

and

XMLPARSEAPI(XML_Parser)
XML_ParserCreate(const XML_Char *encoding);

where

在哪里

#define XMLPARSEAPI(type) XMLIMPORT type XMLCALL

and

#define XMLIMPORT __declspec(dllimport)

If I'm reading that properly, that means that the linkage is consistently __cdecl through XMLCALL-- right? Because, if so, then the warning is superfluous, or am I misinterpreting this?

如果我正确阅读,那意味着链接始终是 __cdecl 通过 XMLCALL-- 对吗?因为,如果是这样,那么警告是多余的,还是我误解了这一点?

回答by Hans Passant

No, it is complaining about __declspec(dllimport)missing from the function definition but present on the function declaration. You ought to take this seriously, it doesn't make sense to declare the function imported from a DLL but alsopresent in your code. You can't have it both ways.

不,它抱怨__declspec(dllimport)函数定义中缺少但存在于函数声明中。您应该认真对待这一点,声明从 DLL 导入但存在于您的代码中的函数是没有意义的。你不能两全其美。

This is usually caused by a missing #define. You edited down the macro definitions, I think, but when building the DLL you usually specify a macro in the build command (/D). So that the declaration of the function uses dllexport instead of dllimport. Which ensures that the function gets exported from the DLL. The client code uses the same .h file but is built without that macro defined. It sees the function declared as dllimport.

这通常是由缺少#define 引起的。我认为您编辑了宏定义,但是在构建 DLL 时,您通常在构建命令 (/D) 中指定一个宏。以便函数的声明使用 dllexport 而不是 dllimport。这确保函数从 DLL 中导出。客户端代码使用相同的 .h 文件,但在构建时没有定义该宏。它看到声明为 dllimport 的函数。

Take a closer look at the XMLIMPORT macro definition, __declspec(dllexport)should be close. Another diagnostic is the set the exported functions, visible with Dumpbin.exe /exports. They should be missing if I guessed correctly.

仔细看看XMLIMPORT 宏定义,__declspec(dllexport)应该差不多了。另一个诊断是设置导出的函数,通过 Dumpbin.exe /exports 可见。如果我猜对了,它们应该会丢失。