尽管使用了EXTERN" C"但是从C ++调用C函数的链接器错误

时间:2013-09-03 12:28:21

标签: c++ c visual-studio-2008 linker-errors

我正在使用这个库 用{C}编写的http://rtmpdump.mplayerhq.hu/librtmp.3.html。 它已经在其所有定义上使用了extern“C”。但是当我从[main.cpp]文件调用函数时,编译器会显示这些错误:

[1>main.obj : error LNK2028: unresolved token (0A00000F) "extern "C" struct RTMP * 
 __cdecl RTMP_Alloc(void)" (?RTMP_Alloc@@$$J0YAPAURTMP@@XZ) referenced in function "int 
 __cdecl main(void)" (?main@@$$HYAHXZ)]

[1>main.obj : error LNK2019: unresolved external symbol "extern "C" struct RTMP * 
 __cdecl RTMP_Alloc(void)" (?RTMP_Alloc@@$$J0YAPAURTMP@@XZ) referenced in function "int 
 __cdecl main(void)" (?main@@$$HYAHXZ)]

我正在使用Visual Studio 2008.并且没有编译错误。 我错过了什么?

1 个答案:

答案 0 :(得分:2)

这是符合症状的情景。两个源文件和一个标题。

<强> first.c

#include "first.h"

struct RTMP     *RTMP_Alloc()
{
        return  (struct RTMP *) 0;
}

<强> second.cpp

#include "first.h"

int main (int argc, char **argv)
{
        RTMP *result;

        result = RTMP_Alloc();

        return  0;
}

<强> first.h

#ifdef __cplusplus
extern "C"
#endif
struct RTMP
{
        int val1;
        int val2;
} ;

struct RTMP     *RTMP_Alloc();

请注意,extern "C"仅适用于结构定义。要纠正它,要么需要在RTMP_Alloc定义之前插入,要么更容易插入大块,放在extern "C" { ... }构造内(注意添加花括号)。