简体   繁体   English

从 C++ 文件执行 C 函数会导致 linker 错误:“与‘C’链接的声明冲突”和“先前的声明‘C++’链接

[英]Executing C functions from C++ file results in linker error: "conflicting declaration with 'C' linkage" and "previous declaration 'C++' linkage

I am attempting to execute a C function from within a C++ file in the Arduino framework.我正在尝试从 ZEE5BCED252AB77EC8D633FDADDAC1 中的 C++ 文件中执行 C function The function I am trying to run, GuiLib_ShowScreen , appears in GuiLib.h and GuiLib.c as follows (the files are massive so for convenience and relevance sake I included only definitions):我正在尝试运行的GuiLib_ShowScreen出现在GuiLib.hGuiLib.c中,如下所示(文件很大,因此为了方便和相关,我只包括定义):

extern void GuiLib_ShowScreen(
      const GuiConst_INT16U StructureNdx,
      GuiConst_INT16S CursorFieldToShow,
      GuiConst_INT8U ResetAutoRedraw)
  { ... } 

And the file from which I am trying to include GuiLib_ShowScreen :我试图从中包含GuiLib_ShowScreen的文件:

#ifndef _DISPLAY_DRIVER_H_
#define _DISPLAY_DRIVER_H_

#include <TFT_eSPI.h>
#include <SPI.h>
#include <ac.h>
#include <stdio.h>
#include "gui/GuiLib.h"

#define USE_DMAA_TO_TFT

extern "C"
{
    void GuiLib_ShowScreen(const GuiConst_INT16U StructureNdx, GuiConst_INT16S CursorFieldToShow, GuiConst_INT8U ResetAutoRedraw);
}

class display_
{
public:
    TFT_eSPI tft;
    bool getStatus();
    void initPWM();
    void writePWM(int duty);
};

#endif

And my main.cpp:还有我的 main.cpp:

#include "display_driver.h"

void TaskPushDMA(void *pvParameters)
{
  while (true)
  {
    GuiLib_ShowScreen(GuiStruct_main_0, GuiLib_NO_CURSOR, GuiLib_RESET_AUTO_REDRAW);
    vTaskDelay(1000);
  }
}

My issue is when compiling, I get a linker error that looks like this:我的问题是在编译时,我收到一个 linker 错误,如下所示:

In file included from src\main.cpp:6:0:
src/display_driver.h:14:129: error: conflicting declaration of 'void GuiLib_ShowScreen(short unsigned int, short int, unsigned char)' with 'C' linkage
     void GuiLib_ShowScreen(const GuiConst_INT16U StructureNdx, GuiConst_INT16S CursorFieldToShow, GuiConst_INT8U ResetAutoRedraw);
                                                                                                                                 ^
In file included from src/display_driver.h:8:0,
                 from src\main.cpp:6:
src/gui/GuiLib.h:1847:13: note: previous declaration with 'C++' linkage
 extern void GuiLib_ShowScreen(
             ^

From what I am getting from this, is it seems to be suggesting that the original linkage is C++, which is strange considering the definition is in a C file.根据我从中得到的信息,它似乎暗示原始链接是 C++,考虑到定义在 C 文件中,这很奇怪。 I've read here as well as previously opened issues here yet no luck.我已经在这里阅读以及以前在这里打开的问题,但没有运气。 I also attempted creating a conditional like such:我还尝试创建这样的条件:

#ifdef __cplusplus
extern "C" int foo(int, int); // C++ compiler sees this
#else
int foo(int, int);            // C compiler sees this
#endif

After trying suggestions from the comments, this cleared out the error.在尝试了评论中的建议后,这清除了错误。 I included the library outside the scope after extern "C" and changed the parameters of GuiLib_ShowScreen() to their native types, defined in the library as #define GuiConst_INT16U unsigned short .我在extern "C"之后将库包含在 scope 之外,并将GuiLib_ShowScreen()的参数更改为它们的本机类型,在库中定义为#define GuiConst_INT16U unsigned short There was a compatibility issue placing the include statement within the scope, and because my original error stated previous declaration with 'C++' linkage it seems apparent that the include statement was interpreted automatically as a C++ header instead of C. There was a compatibility issue placing the include statement within the scope, and because my original error stated previous declaration with 'C++' linkage it seems apparent that the include statement was interpreted automatically as a C++ header instead of C.


extern "C"
{
    void GuiLib_ShowScreen(unsigned short StructureNdx, signed short CursorFieldToShow, unsigned char ResetAutoRedraw);
}

#include "gui/GuiLib.h"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM