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.26k stars 230 forks source link

Multiple definition error when including taskscheduler (from a separate header file.) #51

Closed BlackEdder closed 6 years ago

BlackEdder commented 6 years ago

I tried setting up a simple project (using platformio) and get the following compilation error (I am pretty sure this used to work without a problem.)

ndexing .pioenvs/esp32dev/libd60/libTaskScheduler_ID721.a
Archiving .pioenvs/esp32dev/libFrameworkArduino.a
Indexing .pioenvs/esp32dev/libFrameworkArduino.a
Linking .pioenvs/esp32dev/firmware.elf
.pioenvs/esp32dev/src/main.ino.o: In function `Task::isEnabled()':
/home/edwin/TmpSync/tmpPain/.piolibdeps/TaskScheduler_ID721/src/TaskScheduler.h:308: multiple definition of
 `Task::isEnabled()'
.pioenvs/esp32dev/src/foo1.o:/home/edwin/TmpSync/tmpPain/.piolibdeps/TaskScheduler_ID721/src/TaskScheduler.
h:308: first defined here
.pioenvs/esp32dev/src/main.ino.o: In function `Task::getInterval()':
main.ino.cpp:(.text._ZN4Task11getIntervalEv+0x0): multiple definition of `Task::getInterval()'
.pioenvs/esp32dev/src/foo1.o:foo1.cpp:(.text._ZN4Task11getIntervalEv+0x0): first defined here
.pioenvs/esp32dev/src/main.ino.o: In function `Task::getIterations()':
main.ino.cpp:(.text._ZN4Task13getIterationsEv+0x0): multiple definition of `Task::getIterations()'
.pioenvs/esp32dev/src/foo1.o:foo1.cpp:(.text._ZN4Task13getIterationsEv+0x0): first defined here
.pioenvs/esp32dev/src/main.ino.o: In function `Task::getRunCounter()':
main.ino.cpp:(.text._ZN4Task13getRunCounterEv+0x0): multiple definition of `Task::getRunCounter()'
.pioenvs/esp32dev/src/foo1.o:foo1.cpp:(.text._ZN4Task13getRunCounterEv+0x0): first defined here
.pioenvs/esp32dev/src/main.ino.o: In function `Task::setCallback(void (*)())':
main.ino.cpp:(.text._ZN4Task11setCallbackEPFvvE+0x0): multiple definition of `Task::setCallback(void (*)())
'
...

While debuggin this I found that it happens even with just four files:

src/foo.h

#ifndef FOO1_H
#define FOO1_H

#include "TaskScheduler.h"

int sum(int a, int b);

#endif

src/foo.cpp

#include "foo1.h"

int sum(int a, int b) {
    return a + b;
}

src/main.ino

#include "foo1.h"

void setup() {
  Serial.begin(115200);
}

void loop() {
    sum(100, 100);
}

platformio.ini

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps = TaskScheduler
arkhipenko commented 6 years ago

You need to do it as described in this example: https://github.com/arkhipenko/TaskScheduler/tree/master/examples/Scheduler_example16_Multitab

BlackEdder commented 6 years ago

Thanks @arkhipenko We have now solved this by including "TaskSchedulerDeclarations.h" header in most files, not the "TaskScheduler.h".

arkhipenko commented 6 years ago

Check the new release I just pushed into testing branch:

https://github.com/arkhipenko/TaskScheduler/tree/testing/examples/Scheduler_example21_OO_Callbacks

This version 3.0.0 support dynamic callback method binding via compilation parameter _TASK_OO_CALLBACKS

In essence, you create each type of task as a separate class, inherited from the Task class, overload Callback(), OnEnable() and OnDisable() methods (latter 2 optionally), and have full access to your task context without having to deal with LTS.

Cheers, Anatoli


From: Edwin van Leeuwen notifications@github.com Sent: Thursday, March 8, 2018 6:44 AM To: arkhipenko/TaskScheduler Cc: Anatoli Arkhipenko; Mention Subject: Re: [arkhipenko/TaskScheduler] Multiple definition error when including taskscheduler (from a separate header file.) (#51)

Closed #51https://github.com/arkhipenko/TaskScheduler/issues/51.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/arkhipenko/TaskScheduler/issues/51#event-1510961050, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AATGTXOq-iMA0znQsnXLk4yZ-d4DY7CAks5tcRmogaJpZM4SbAC4.