TaskScheduler-Library 的预处理不起作用 - 多重定义错误

preprocessing for TaskScheduler-Library doesn't work - multiple definition Error

我在为整个图书馆获取多个定义 errors 时遇到了问题。 只要我将 TaskScheduler 库包含在另一个 header-file 中(.ino 除外),我就会得到 error.

澄清一下:以下不是我要修复的实际代码!我将它减少到仍然有 error(在 Eclipse sloeber 和 Arduino IDE 中测试)的最小值:

Project.ino :

#include <TaskScheduler.h>
#include "WifiHelper.h"

void setup(){}
void loop(){}

WifiHelper.h :

#ifndef WIFIHELPER_H_
#define WIFIHELPER_H_

#include <TaskScheduler.h>

#endif /* WIFIHELPER_H_ */

WifiHelper.cpp :

#include "Wifihelper.h"

您可以下载项目here

那么为什么我需要在此处包含 TaskScheduler.h两次? 那么,class WifiHelper.h 接收到一个 Scheduler 类型的指针,并在其构造函数 WifiHelper::WifiHelper(std::shared_prt<Scheduler> scheduler){...} 中的文件 WifiHelper.cpp 中用它启动一个 Task

TaskScheduler.h 有一个预处理器语句,我真的不明白为什么我会在这里遇到这个问题。在我原来的项目中,我使用了如上所述的其他几个库,一切正常。总之,我想知道这是否是图书馆的问题...

提前谢谢你:)

errormessage的一部分:

'Starting combiner'
"C:/Users/Mimi/Documents/Arduino/hardware/espressif/esp32/tools/xtensa/esp32-elf/bin/xtensa-esp32-elf-gcc" -nostdlib "-LC:/Users/Mimi/Documents/Arduino/hardware/espressif/esp32/tools/sdk/lib" "-LC:/Users/Mimi/Documents/Arduino/hardware/espressif/esp32/tools/sdk/ld" -T esp32_out.ld -T esp32.common.ld -T esp32.rom.ld -T esp32.peripherals.ld -T esp32.rom.spiram_incompatible_fns.ld -u ld_include_panic_highint_hdl -u call_user_start_cpu0 -Wl,--gc-sections -Wl,-static -Wl,- undefined=uxTopUsedPriority -u __cxa_guard_dummy -u __cxx_fatal_exception -Wl,--start-group    .\WifiHelper.cpp.o .\sloeber.ino.cpp.o   "C:/Users/Mimi/eclipse-workspaces/esp32/Project2/Release/arduino.ar" -lgcc -lopenssl -lbtdm_app -lfatfs -lwps -lcoexist -lwear_levelling -lhal -lnewlib -ldriver -lbootloader_support -lpp -lmesh -lsmartconfig -ljsmn -lwpa -lethernet -lphy -lapp_trace -lconsole -lulp -lwpa_supplicant -lfreertos -lbt -lmicro-ecc -lcxx -lxtensa-debug-module -lmdns -lvfs -lsoc -lcore -lsdmmc -lcoap -ltcpip_adapter -lc_nano -lrtc -lspi_flash -lwpa2 -lesp32 -lapp_update -lnghttp -lspiffs -lespnow -lnvs_flash -lesp_adc_cal -llog -lexpat -lm -lc -lheap -lmbedtls -llwip -lnet80211 -lpthread -ljson -lstdc++ -Wl,--end-group -Wl,-EL -o "C:/Users/Mimi/eclipse-workspaces/esp32/Project2/Release/Project2.elf" C:/Users/Mimi/eclipse-workspaces/esp32/Project2/Release/arduino.ar
.\sloeber.ino.cpp.o: In function `Task::isEnabled()':
C:\Users\Mimi\Documents\Arduino\libraries\TaskScheduler\src/TaskScheduler.h:312: multiple definition of `Task::isEnabled()'
.\WifiHelper.cpp.o:C:\Users\Mimi\Documents\Arduino\libraries\TaskScheduler\src/TaskScheduler.h:312: first defined here
.\sloeber.ino.cpp.o: In function `Task::getInterval()':
sloeber.ino.cpp:(.text._ZN4Task11getIntervalEv+0x0): multiple definition of `Task::getInterval()'
.\WifiHelper.cpp.o:WifiHelper.cpp:(.text._ZN4Task11getIntervalEv+0x0): first defined here
.\sloeber.ino.cpp.o: In function `Task::getIterations()':

等等 - 如果您想查看完整消息,请单击 here...不要被最后的 "Project2.ino" 搞糊涂了。我只是在另一个具有相同文件的项目中尝试了它。

在 C/C++ 中,如果您在 2 个单独的 "translation modules"(即“.cpp”文件)中定义一个函数,您将收到此错误,因为链接器不知道是哪一个用于最终程序。

您 Project.ino 似乎编译为它自己的翻译模块 - "sloeber.ino.cpp",因此如果您在 WifiHelper.cppsloeber.ino.cpp 中包含相同的函数定义,您将收到此错误。

首先确保您没有包含两次内容。 为了绝对确定你可以在你的 #include:

周围添加守卫
#ifndef TASK_SCHEDULER_H_INCLUDED
#define TASK_SCHEDULER_H_INCLUDED
#include <TaskScheduler.h>
#endif // TASK_SCHEDULER_H_INCLUDED

在这里查看源代码 - https://github.com/arkhipenko/TaskScheduler/blob/master/src/TaskScheduler.h

这显然不是要包含两次。

但是那里还有另一个文件 - TaskSchedulerDeclarations.h,它可能会被包含多次,所以也许可以尝试在您的其他文件中使用该文件。

如果这没有帮助,还有其他解决方法:

  1. 将库函数定义从 .h 文件移动到 .cpp 文件,并且仅将函数声明保留在 .h 文件中(尽管这可能需要大量工作)
  2. 或在函数定义前添加 "static",这会将它们呈现为本地翻译模块(单个 cpp 文件),在这种情况下,在整个程序。