C2732 - Linkage specification error
Asked Answered
O

4

11

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.

Ostap answered 22/11, 2013 at 7:45 Comment(6)
What is SD_API_STATUS? And is there more written in the error log? Please update the question to include the complete (and unedited) log.Hexachlorophene
Are you sure there are no other declarations/definitions of SdhcInitialize?Eyestalk
I'm assuming that is a status code return type, not a macro with declaration specifiers.Palaeontography
@JoachimPileborg It is just a structure. But, for simplicity I have updated my question with void. Also, I have included the unedited log. Thanks.Ostap
@Eyestalk In the file_1.cpp, No. There is only one declaration of it. But, in one .c file, that function has been declared without "C" in extern. Does it involve in this?Ostap
Are your other declarations missing extern or having static? Are you including .c files in .cpp files (like #include "file_2.c" in some C++ file). Why not have header file_2.h` with needed extern declarations, which would be included to other files, instead of having externs all over the place like you have now?Eyestalk
P
14

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"
      }
    
Patella answered 22/11, 2013 at 8:48 Comment(2)
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?Ostap
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.Patella
I
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);
}
Instal answered 22/11, 2013 at 8:23 Comment(2)
Thanks. But, even removing that extern doesn't make any changes to that error.Ostap
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 ?Imperial
D
2

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....

Debag answered 17/3, 2014 at 2:10 Comment(1)
This work for me, thanks! (Although I don't really know why Q_Q)Minutely
B
0

I have solved this as follows (the other solutions did not work for me):

In the file vector.cc:

#define __INVECTOR //solves 
#include "vector.h"

In vector.h:

#ifndef __INVECTOR
void function(...etc..);
#endif

This way the declaration isn't read unless we want to call the function from a different file.

Beaird answered 31/12, 2021 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.