Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C2732 - Linkage specification error

I'm using VS2008. I'm getting the following error.

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)"}

file_1.cpp

extern "C"
{
   // some extern declarations
   extern void SdhcInitialize(DWORD slot);
}

file_2.c

void SdhcInitialize(DWORD slot)
{
//some code
}

Please guide me how to resolve.

like image 884
Gomu Avatar asked Nov 22 '13 07:11

Gomu


2 Answers

I'm guessing that you have a header that contains a prototype for the SdhcInitialize() function, and that the header was written for use by C programs. So for example, the header file might include something like the following line:

SD_API_STATUS SdhcInitialize(DWORD slot);

without being enclosed in an extern "C" {} block (since the header is intended for C programs).

Additionally, I suspect that this header is being included - directly or indirectly - by file_1.cpp

This means that the header cannot be included in a C++ program without some additional work being done, otherwise the C++ program will see the declaration as meaning that SdhcInitialize() has C++ linkage.

You have two reasonable approaches to fixing this:

  • if you can modify the header, add the following lines around the declarations in the header:

      #if __cplusplus
      extern "C" {
      #endif
    
      // declarations go here
    
      #if __cplusplus
      }
      #endif
    

    This way, C++ files will have the declarations enclosed in a extern "C" linkage block, while C program will not see the extern "C" bits (which would otherwise confuse the C compiler).

    I think an argument can be made that all C headers should include something like those lines so that the C functions can be consumed by C++ programs without hassle.

  • if you cannot modify the header for some reason, you can work around the problem by including the header in C++ files like so:

      extern "C" {
      #include "Sdhc-header.h"
      }
    
like image 117
Michael Burr Avatar answered Oct 12 '22 14:10

Michael Burr


If you surround a set of function declaration by extern "C" { ... }, you don't need to use an additionnal externkeyword in front of the function identifier.

extern "C"
{
   // some extern declarations
   SD_API_STATUS SdhcInitialize(DWORD slot);
}
like image 43
alexbuisson Avatar answered Oct 12 '22 13:10

alexbuisson