You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TaskScheduler库预处理失效:出现多重定义错误

Fixing Multiple Definition Errors with TaskScheduler Library in Arduino

Let's break down why you're hitting these multiple definition errors and how to resolve them cleanly.

The Root Cause

The TaskScheduler library's header file (TaskScheduler.h) includes implementations for many member functions directly inside the class definition or in the header without the inline keyword. When you include this header in multiple compilation units (your .ino file, which gets compiled to a .cpp file, plus WifiHelper.cpp), each unit generates its own copy of these function definitions. During the linking phase, the linker sees duplicate copies of the same functions (like Task::isEnabled() or Task::getInterval()) and throws an error.

Your other libraries don't trigger this issue because they either:

  • Place function implementations in separate .cpp files instead of headers, or
  • Mark header-only function implementations with the inline keyword, which tells the linker to ignore duplicate copies of the function.

The Best Fix: Use Forward Declarations

Since your WifiHelper class only needs a pointer to Scheduler (not the full class definition) in its header, you can replace the direct #include <TaskScheduler.h> with a forward declaration. This avoids pulling in the entire library header in multiple places, while still letting you use the pointer type.

Here's how to adjust your code step by step:

1. Update WifiHelper.h

#ifndef WIFIHELPER_H_
#define WIFIHELPER_H_
#include <memory> // Required for std::shared_ptr

// Forward declare the Scheduler class instead of including the full header
class Scheduler;

class WifiHelper {
public:
    // Your constructor declaration stays unchanged
    WifiHelper(std::shared_ptr<Scheduler> scheduler);

    // Add other member declarations here as needed
};
#endif /* WIFIHELPER_H_ */

2. Update WifiHelper.cpp

Now include the TaskScheduler header in the .cpp file, where the actual implementation needs the full class definition:

#include "WifiHelper.h"
// Include the library header here, only once for this compilation unit
#include <TaskScheduler.h>

WifiHelper::WifiHelper(std::shared_ptr<Scheduler> scheduler) {
    // You can now use scheduler and Task-related classes here without issues
}

3. Keep Your Project.ino As-Is

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

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

Alternative (Less Ideal) Fix: Modify the Library

If forward declarations don't fit your use case, you could edit the TaskScheduler.h file to add the inline keyword to all member functions implemented inside the class or in the header. For example:

class Task {
public:
    // Add inline to function implementations
    inline bool isEnabled() const { return _enabled; }
    inline uint32_t getInterval() const { return _interval; }
    // ... repeat for other affected functions
};

Note: This requires modifying the library source, which will get overwritten if you update the library later.

Verify the Fix

After making these changes, recompile your project in either Arduino IDE or Eclipse Sloeber— the multiple definition errors should disappear.

内容的提问来源于stack exchange,提问作者Mina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:19:30