C语言 不允许定义 dllimport 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7657552/
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
definition of dllimport function not allowed
提问by user979791
While compiling a C code, I'm getting the following error:
在编译 C 代码时,我收到以下错误:
c:\users\kbarman\documents\mser\vlfeat-0.9.13-try\mser\stringop.c(71): error C2491: 'vl_string_parse_protocol' : definition of dllimport function not allowed
In the file stringop.c, I have the following function:
在文件 stringop.c 中,我有以下功能:
VL_EXPORT char *
vl_string_parse_protocol (char const *string, int *protocol)
{
char const * cpt ;
int dummy ;
/* handle the case prot = 0 */
if (protocol == 0)
protocol = &dummy ;
/* look for :// */
cpt = strstr(string, "://") ;
if (cpt == 0) {
*protocol = VL_PROT_NONE ;
cpt = string ;
}
else {
if (strncmp(string, "ascii", cpt - string) == 0) {
*protocol = VL_PROT_ASCII ;
}
else if (strncmp(string, "bin", cpt - string) == 0) {
*protocol = VL_PROT_BINARY ;
}
else {
*protocol = VL_PROT_UNKNOWN ;
}
cpt += 3 ;
}
return (char*) cpt ;
}
And VL_EXPORT is defined as follows:
VL_EXPORT 定义如下:
# define VL_EXPORT extern "C" __declspec(dllimport)
Can somebody please tell me what is causing this error and how I can get rid of it?
有人可以告诉我是什么导致了这个错误以及我如何摆脱它?
回答by Roman R.
As documentation states, dllimportfunction are not allowed to have a body right there.
正如文档所述,dllimport函数不允许在那里有一个主体。
[...] functions can be declared as dllimports but not definedas dllimports.
[...] 函数可以声明为 dllimports,但不能定义为 dllimports。
// function definition
void __declspec(dllimport) funcB() {} // C2491
// function declaration
void __declspec(dllimport) funcB(); // OK
回答by David Heffernan
You are saying that the function is external, defined in a Dll. And then you are defining it in your code. This is illegal since is has to be one or the other, but not both external and internal.
您是说该函数是外部的,在 Dll 中定义。然后你在你的代码中定义它。这是非法的,因为必须是其中之一,但不能同时是外部和内部。
My guess is that you simply need to change dllimport to dllexport. I assume that you are building this code into a library.
我的猜测是您只需将 dllimport 更改为 dllexport。我假设您正在将此代码构建到库中。

