C2732 - Linkage specification error

21,860

Solution 1

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

Solution 2

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

Solution 3

When you try to include the "some header files of C" file in "C++ file"(the header file has some where extern "C" for some functions).

include the header earlier will solve the problem.

e.g. Try to move #include "myHeader.h" on the top lines of your C++ file.

This solves my problems.

Hope it helps....

Share:
21,860
Gomu
Author by

Gomu

Programming Languages: C, C++ Source Control Tool: GIT Platform: Linux, Windows VC++ "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John Woods

Updated on July 09, 2022

Comments

  • Gomu
    Gomu almost 2 years

    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.

  • Gomu
    Gomu over 10 years
    Thanks. But, even removing that extern doesn't make any changes to that error.
  • Gomu
    Gomu over 10 years
    Thanks. I have included __cplusplus as mentioned, but error persists. You have mentioned I suspect that this header is being included - directly or indirectly - by file_1.cpp. Actually, I have another file which has .c extension, wherein I have just declared extern without "C". Is it any kind of reason behind this error?
  • Michael Burr
    Michael Burr over 10 years
    Does bootpart_e.cpp include some file (including indirectly) that declares SdhcInitialize(). If you have trouble determining this, using the /P option will send the preprocessor output to a file and you can search that file for all instances of SdhcInitialize to see where the declarations are coming from.
  • Mouin
    Mouin over 5 years
    I got the same warning, when I used a "double extern", the warning disappear when I remove one extern, but WHY ?, the extern "C" is different than extern ?
  • Coco Yen
    Coco Yen almost 4 years
    This work for me, thanks! (Although I don't really know why Q_Q)