简体   繁体   中英

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

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 :

#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:

#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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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