2011-10-05 193 views
4

在编译的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 

在文件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 ; 
} 

而VL_EXPORT定义如下:

#  define VL_EXPORT extern "C" __declspec(dllimport) 

有人可以告诉我是什么原因造成这个错误,我怎么能得到RI它的?

+0

从#define查找,有一个#ifdef可以在dllexport和dllimport之间进行选择。你需要在这里定义这个宏。 –

回答

3

由于documentation states,dllimport函数不允许在那里有一个正文。

[...] functions can be declared as dllimports but not defined as dllimports.

// function definition 
void __declspec(dllimport) funcB() {} // C2491 

// function declaration 
void __declspec(dllimport) funcB(); // OK 
+2

如果你声明一个dllimport,那么你不应该定义它。这是一个外部。 –

+0

正确,谢谢。定义不仅是分开的,而且是外部的。 –

1

您是说的功能外,在DLL中定义。然后你在代码中定义它。这是非法的,因为它必须是一个或另一个,但不是外部的和内部的。

我的猜测是,您只需要将dllimport更改为dllexport。我假设你正在将这些代码构建到一个库中。

+0

我没有在我的主代码中实际定义它。 main.c调用该函数vl_string_parse_protocol,该函数在名为stringop.h的头文件中声明,并在stringop.c中定义。 – user979791

+0

哪个翻译单元定义它并不重要。如果你声明它是外部的,那么你不能定义它。 –