I'm writing an Arduino app (Using platformIO in VSCode), and including an external CAN library (FlexCAN_T4). I want to define all of my protocol / message handler callbacks in a separate file (protocol.cpp), and refer to them from my main file (main.cpp). So I create a header file (protocol.h) with all my function signatures, and include protocol.h from both protocol.cpp and main.cpp.
But I'm getting "multiple definition of `flexcan_isr_can1()'" (which is a function defined in a referenced library, FlexCAN_T4). I can't figure out how to get rid of this error, and still have all my function signatures and constants and whatnot work properly.
Sample error messages (one for each non-class function defined in FlexCan_T4.h):
.pio/build/teensy40/src/protocol.cpp.o: In function `flexcan_isr_can1()':
protocol.cpp:(.text._Z16flexcan_isr_can1v+0x0): multiple definition of `flexcan_isr_can1()'
.pio/build/teensy40/src/main.cpp.o:main.cpp:(.text._Z16flexcan_isr_can1v+0x0): first defined here
/Users/pdesrosiers/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/bin/ld: Disabling relaxation: it will not work with multiple definitions
protocol.h
#pragma once
#include <FlexCAN_T4.h>
const uint8_t myConst = 100;
extern FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> flexCAN;
void myFunction();
protocol.cpp
#include "protocol.h"
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> flexCAN;
void myFunction()
{
//one of a hundred functions related to protocol, that should be defined outside of main.cpp.
flexCan.doSomething(myConst);
}
main.cpp
#include "protocol.h"
#include <Arduino.h>
void setup()
{
//some setup stuff...
}
void loop()
{
myFunction();
}
In a certain sense, these errors make sense, since protocol.h (and, transitively, FlexCAN_T4.h) is being #included multiple times.
But how can I keep this nice organization of code, which my callbacks defined outside main.cpp? What's the correct way to include these dependencies?