arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.24k stars 226 forks source link

Compilation Link error with multiple includes #142

Closed PPCM closed 2 years ago

PPCM commented 2 years ago

Hello,

I would to make my dev OOP, so each object need to include <TaskScheduler.h> But it fails during the link process. The platform used is a WEMOS D1 MINI ESP32

Here is a working example

config.h

#ifndef __CONFIG_H__
#define __CONFIG_H__

#define Test 123

#endif

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <TaskScheduler.h>

#include "config.h"

#endif

main.c

#include <Arduino.h>

#include "main.h"

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

The compile complete correctly, it produce only one warning (already an issue)

Now if I include <TaskScheduler.h> in an other file, the linking process fails for multiple definition of elements In the previous example modify following files main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <TaskScheduler.h>

#include "config.h"

#include "A.h"

#endif

A.h

#ifndef __A_H__
#define __A_H__

#include <TaskScheduler.h>

#include "config.h"

#endif

A.cpp

#include "A.h"

It produces the following error:

.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::isEnabled()':
C:\Users\ppcm\Documents\PlatformIO\Projects\Test/.pio\libdeps\wemos_d1_mini32\TaskScheduler\src/TaskScheduler.h:458: multiple definition of `Task::isEnabled()'
.pio\build\wemos_d1_mini32\src\A.cpp.o:C:\Users\ppcm\Documents\PlatformIO\Projects\Test/.pio\libdeps\wemos_d1_mini32\TaskScheduler\src/TaskScheduler.h:458: first defined here
.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::getInterval()':
main.cpp:(.text._ZN4Task11getIntervalEv+0x0): multiple definition of `Task::getInterval()'
.pio\build\wemos_d1_mini32\src\A.cpp.o:A.cpp:(.text._ZN4Task11getIntervalEv+0x0): first defined here
.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::getIterations()':
main.cpp:(.text._ZN4Task13getIterationsEv+0x0): multiple definition of `Task::getIterations()'
.pio\build\wemos_d1_mini32\src\A.cpp.o:A.cpp:(.text._ZN4Task13getIterationsEv+0x0): first defined here
.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::getRunCounter()':
main.cpp:(.text._ZN4Task13getRunCounterEv+0x0): multiple definition of `Task::getRunCounter()'
.pio\build\wemos_d1_mini32\src\A.cpp.o:A.cpp:(.text._ZN4Task13getRunCounterEv+0x0): first defined here
.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::setCallback(void (*)())':
main.cpp:(.text._ZN4Task11setCallbackEPFvvE+0x0): multiple definition of `Task::setCallback(void (*)())'
.pio\build\wemos_d1_mini32\src\A.cpp.o:A.cpp:(.text._ZN4Task11setCallbackEPFvvE+0x0): first defined here
.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::setOnEnable(bool (*)())':
main.cpp:(.text._ZN4Task11setOnEnableEPFbvE+0x0): multiple definition of `Task::setOnEnable(bool (*)())'
.pio\build\wemos_d1_mini32\src\A.cpp.o:A.cpp:(.text._ZN4Task11setOnEnableEPFbvE+0x0): first defined here
.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::setOnDisable(void (*)())':
main.cpp:(.text._ZN4Task12setOnDisableEPFvvE+0x0): multiple definition of `Task::setOnDisable(void (*)())'
.pio\build\wemos_d1_mini32\src\A.cpp.o:A.cpp:(.text._ZN4Task12setOnDisableEPFvvE+0x0): first defined here
.pio\build\wemos_d1_mini32\src\main.cpp.o: In function `Task::reset()':
main.cpp:(.text._ZN4Task5resetEv+0x0): multiple definition of `Task::reset()'
.pio\build\wemos_d1_mini32\src\A.cpp.o:A.cpp:(.text._ZN4Task5resetEv+0x0): first defined here
[...]

This is not the complete error because it is too long, but the idea is here

Is there something wrong in the declaration of prototypes of TaskScheduler? Thanks for your help

arkhipenko commented 2 years ago

Please look at how to implement this here:

For Arduino IDE https://github.com/arkhipenko/TaskScheduler/tree/master/examples/Scheduler_example21_OO_Callbacks

For Platform io: https://github.com/arkhipenko/TaskScheduler/tree/master/examples/Scheduler_example27_PlatformIO