gpt4 book ai didi

c++ - C2732 - 链接规范错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:20 25 4
gpt4 key购买 nike

我正在使用 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)"}

file_1.cpp

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

file_2.c

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

请指导我如何解决。

最佳答案

我猜您有一个包含 SdhcInitialize() 函数原型(prototype)的 header ,并且该 header 是为 C 程序编写的。因此,例如,头文件可能包含类似以下行的内容:

SD_API_STATUS SdhcInitialize(DWORD slot);

没有包含在 extern "C"{} block 中(因为 header 用于 C 程序)。

此外,我怀疑此 header 被 file_1.cpp

直接或间接地包含在内

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

您有两种合理的方法来解决此问题:

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

      #if __cplusplus
    extern "C" {
    #endif

    // declarations go here

    #if __cplusplus
    }
    #endif

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

    我认为可以提出一个论点,即所有 C header 都应该包含类似这些行的内容,以便 C++ 程序可以毫不费力地使用 C 函数。

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

      extern "C" {
    #include "Sdhc-header.h"
    }

关于c++ - C2732 - 链接规范错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com