I want to have a macro defined in a class. I wish to define it first in my main, but use an #ifndef to create it if the user chooses not to. For some reason it would seem like the class is being linked before the main, so my #define in main is not being used. TIA
My main ino
#define TEST_MACRO 4
#include "test.h"
test testclass;
void setup() {
Serial.begin(9600);
testclass.showValue();
}
void loop() {
}
test.h
#ifndef TEST_MACRO
#define TEST_MACRO 5
#endif
#include "Arduino.h"
class test
{
public:
test();
void showValue();
private:
uint16_t testval = TEST_MACRO;
};
test.cpp
#include "test.h"
test::test()
{
;
}
void test::showValue()
{
Serial.println(testval);
}
Expected Result "4" Result "5"
However, if I do this with a test.h file include and no class, it all works as expected.
testvalis intest::testat which point the macro is defined to 5 so that's the value you get. You've violated the ODR though so it's undefined behaviouruint16_t testval = 4is a definition, your two cpp files contain different versions of this definition