Executing C functions from C++ file results in linker error: "conflicting declaration with 'C' linkage" and "previous declaration 'C++' linkage
Executing C functions from C++ file results in linker error: "conflicting declaration with 'C' linkage" and "previous declaration 'C++' linkage
我正在尝试从 Arduino 框架中的 C++ 文件中执行 C 函数。我尝试 运行、GuiLib_ShowScreen
的函数出现在 GuiLib.h
和 GuiLib.c
中,如下所示(文件很大,为了方便和相关性,我只包含了定义):
extern void GuiLib_ShowScreen(
const GuiConst_INT16U StructureNdx,
GuiConst_INT16S CursorFieldToShow,
GuiConst_INT8U ResetAutoRedraw)
{ ... }
以及我要从中包含的文件 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
还有我的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);
}
}
我的问题是在编译时出现链接器错误,如下所示:
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(
^
从我得到的信息来看,它似乎暗示原始链接是 C++,考虑到定义在 C 文件中,这很奇怪。我已经阅读了 here 以及之前在这里打开的问题,但没有运气。我还尝试创建这样的条件:
#ifdef __cplusplus
extern "C" int foo(int, int); // C++ compiler sees this
#else
int foo(int, int); // C compiler sees this
#endif
在尝试了评论中的建议后,这清除了错误。我在 extern "C"
之后将库包含在范围之外,并将 GuiLib_ShowScreen()
的参数更改为其本机类型,在库中定义为 #define GuiConst_INT16U unsigned short
。在范围内放置 include 语句存在兼容性问题,因为我的原始错误指出 previous declaration with 'C++' linkage
很明显 include 语句被自动解释为 C++ header 而不是 C.
extern "C"
{
void GuiLib_ShowScreen(unsigned short StructureNdx, signed short CursorFieldToShow, unsigned char ResetAutoRedraw);
}
#include "gui/GuiLib.h"
我正在尝试从 Arduino 框架中的 C++ 文件中执行 C 函数。我尝试 运行、GuiLib_ShowScreen
的函数出现在 GuiLib.h
和 GuiLib.c
中,如下所示(文件很大,为了方便和相关性,我只包含了定义):
extern void GuiLib_ShowScreen(
const GuiConst_INT16U StructureNdx,
GuiConst_INT16S CursorFieldToShow,
GuiConst_INT8U ResetAutoRedraw)
{ ... }
以及我要从中包含的文件 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
还有我的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);
}
}
我的问题是在编译时出现链接器错误,如下所示:
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(
^
从我得到的信息来看,它似乎暗示原始链接是 C++,考虑到定义在 C 文件中,这很奇怪。我已经阅读了 here 以及之前在这里打开的问题,但没有运气。我还尝试创建这样的条件:
#ifdef __cplusplus
extern "C" int foo(int, int); // C++ compiler sees this
#else
int foo(int, int); // C compiler sees this
#endif
在尝试了评论中的建议后,这清除了错误。我在 extern "C"
之后将库包含在范围之外,并将 GuiLib_ShowScreen()
的参数更改为其本机类型,在库中定义为 #define GuiConst_INT16U unsigned short
。在范围内放置 include 语句存在兼容性问题,因为我的原始错误指出 previous declaration with 'C++' linkage
很明显 include 语句被自动解释为 C++ header 而不是 C.
extern "C"
{
void GuiLib_ShowScreen(unsigned short StructureNdx, signed short CursorFieldToShow, unsigned char ResetAutoRedraw);
}
#include "gui/GuiLib.h"