I hate this error because it's so vague. Here's example code that triggers it:
gives:
Why? Well, without going deep into the details, it's because foo::alpha is included in the class definition i.e. it's an inline function. A class definition is allowed to be included in multiple source files, so inline functions must be weak to avoid triggering multiple definition errors. This also applies to the local data used in those functions. Long story short, this means the compiler tries to create a .progmem section in the output file for this source that is both globally unique and weak; these are opposing traits so that's where the section type conflict comes from.
The solution is to not make the class function inline:
TADA! No more section conflicts.
C++:
#include <Arduino.h>
static const char g_abc[] PROGMEM = "ab";
class foo {
public:
static const char& alpha(int x) {
static const char g_alpha[2] PROGMEM = {3, 1};
return g_alpha[x&1];
}
};
void setup() {
}
void loop() {
int x = Serial.read();
if (x >= 0) {
Serial.write(g_abc[x&1]);
Serial.write(foo::alpha(x));
}
}
Compilation error: 'g_abc' causes a section type conflict with 'g_alpha' in section '.progmem'
Why? Well, without going deep into the details, it's because foo::alpha is included in the class definition i.e. it's an inline function. A class definition is allowed to be included in multiple source files, so inline functions must be weak to avoid triggering multiple definition errors. This also applies to the local data used in those functions. Long story short, this means the compiler tries to create a .progmem section in the output file for this source that is both globally unique and weak; these are opposing traits so that's where the section type conflict comes from.
The solution is to not make the class function inline:
C++:
#include <Arduino.h>
static const char g_abc[] PROGMEM = "ab";
class foo {
public:
static const char& alpha(int x);
};
const char& foo::alpha(int x) {
static const char g_alpha[2] PROGMEM = {3, 1};
return g_alpha[x&1];
}
void setup() {
}
void loop() {
int x = Serial.read();
if (x >= 0) {
Serial.write(g_abc[x&1]);
Serial.write(foo::alpha(x));
}
}