TaskScheduler: TaskScheduler seems not compatible with multiple object source files (aka tabs in IDE)

My project will be rather big and I need to have it splitted between many .cpp files. The source code for your library is included in one big header file, declarations together with definitions. What should I do, to stop linker from complaining about

/home/Adama-docs/Adam/MyDocs/Programowanie/Arduino/libraries/TaskScheduler/src/TaskScheduler.h:387: multiple definition of `StatusRequest::signal(int)'
./.ino.cpp.o:/home/Adama-docs/Adam/MyDocs/Programowanie/Arduino/libraries/TaskScheduler/src/TaskScheduler.h:387: first defined here
./file2.cpp.o: In function `StatusRequest::signal(int)':
/home/Adama-docs/Adam/MyDocs/Programowanie/Arduino/libraries/TaskScheduler/src/TaskScheduler.h:387: multiple definition of `StatusRequest::signalComplete(int)'
./.ino.cpp.o:/home/Adama-docs/Adam/MyDocs/Programowanie/Arduino/libraries/TaskScheduler/src/TaskScheduler.h:387: first defined here
./file2.cpp.o: In function `StatusRequest::signal(int)':
/home/Adama-docs/Adam/MyDocs/Programowanie/Arduino/libraries/TaskScheduler/src/TaskScheduler.h:387: multiple definition of `Task::reset()'
./.ino.cpp.o:/home/Adama-docs/Adam/MyDocs/Programowanie/Arduino/libraries/TaskScheduler/src/TaskScheduler.h:387: first defined here
./file2.cpp.o: In function `StatusRequest::signal(int)':

To reproduce:

  1. Open new sketch, and put there
#include "header.hpp"

void setup()
{
    Serial.begin(9600);
    Serial.println(myfunc());
    Serial.println(globalx);
}

void loop() {}

  1. Add a new tab header.hpp with
#pragma once

#include <stdint.h>
#include <TaskScheduler.h>
extern uint8_t globalx;


uint8_t myfunc();

  1. Add a new tab file2.cpp with
#include "header.hpp"

uint8_t globalx = 42;

uint8_t myfunc()
{
    return 23;
}

  1. Try to compile and get the errors.

If you remove the line #include <TaskScheduler.h> from the header.hpp, everything compiles, but without your library.

I guess you’d need to split the library into proper decraration header and the definition (.cpp) file. Do you have plans for that?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

Switching the include in other tabs from #include <TaskScheduler.h> to #include <TaskSchedulerDeclarations.h> as suggested by Anatoli worked!

I must have missed this somewhere in the documentation. Suggest adding a bold (highly obvious) comment so others can avoid my error.

Never needed to do this before, always something new to learn 😃

Thank you so much.