C2732 - 链接规范错误

Gom*_*omu 6 c c++ extern visual-studio-2008

我正在使用VS2008.我收到以下错误.

BUILD: [02:0000000295:ERRORE] c:\wince700\platform\am33x_bsp\src\bootloader\bootpart\bootpart_e.cpp(61) : error C2732: linkage specification contradicts earlier specification for 'SdhcInitialize' {log="C:\WINCE700\platform\AM33X_BSP\SRC\BOOTLOADER\bldsys.log(103)"}
BUILD: [02:0000000297:ERRORE] NMAKE : fatal error U1077: 'C:\WINCE700\sdk\bin\i386\ARM\cl.EXE' : return code '0x2' {log="C:\WINCE700\platform\AM33X_BSP\SRC\BOOTLOADER\bldsys.log(104)"}
BUILD: [02:0000000299:ERRORE]  clean TargetCompilePass  -nologo BUILDMSG=Stop.  BUILDROOT=C:\WINCE700\platform\AM33X_BSP CLEANBUILD=1 NOLINK=1 NOPASS0=1 failed - rc = 2. {log="C:\WINCE700\platform\AM33X_BSP\SRC\BOOTLOADER\bldsys.log(105)"}
Run Code Online (Sandbox Code Playgroud)

file_1.cpp

extern "C"
{
   // some extern declarations
   extern void SdhcInitialize(DWORD slot);
}
Run Code Online (Sandbox Code Playgroud)

file_2.c

void SdhcInitialize(DWORD slot)
{
//some code
}
Run Code Online (Sandbox Code Playgroud)

请指导我如何解决.

Mic*_*urr 12

我猜你有一个包含该SdhcInitialize()函数原型的头文件,并且该头文件是为C程序使用而编写的.例如,头文件可能包含以下行:

SD_API_STATUS SdhcInitialize(DWORD slot);
Run Code Online (Sandbox Code Playgroud)

没有被包含在一个extern "C" {}块中(因为标题是用于C程序).

另外,我怀疑这个标题是直接或间接包含的 file_1.cpp

这意味着如果没有做一些额外的工作,标题就不能包含在C++程序中,否则C++程序会将声明视为SdhcInitialize()具有C++链接的含义.

你有两种合理的方法来解决这个问题:

  • 如果您可以修改标题,请在标题中的声明周围添加以下行:

      #if __cplusplus
      extern "C" {
      #endif
    
      // declarations go here
    
      #if __cplusplus
      }
      #endif
    
    Run Code Online (Sandbox Code Playgroud)

    这样,C++文件将包含在extern "C"链接块中的声明,而C程序将不会看到这些extern "C"位(否则会混淆C编译器).

    我认为可以做出一个论点,即所有C头应该包含类似那些行的东西,这样C函数就可以毫不费力地使用C函数.

  • 如果由于某种原因无法修改标题,可以通过在C++文件中包含标题来解决此问题,如下所示:

      extern "C" {
      #include "Sdhc-header.h"
      }
    
    Run Code Online (Sandbox Code Playgroud)