(I can't delete this post...) but you can ignore it if you like since, as you can see from the next post, I fixed it.
I have tried for several hours to figure this out... over six now... and getting nowhere. At link time I get this...
1 2 3 4 5 6 7 8
c:/users/peted/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio\build\um_feathers3\src\pulseCount.cpp.o:C:\Users\peted\OneDrive\Documents\PlatformIO\Swarm\Tzatziki/src/mysensors.h:106: multiple definition of `PulseSensor2'; .pio\build\um_feathers3\src\initialize.cpp.o:C:\Users\peted\OneDrive\Documents\PlatformIO\Swarm\Tzatziki/src/mysensors.h:106: first defined here
c:/users/peted/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio\build\um_feathers3\src\pulseCount.cpp.o:C:\Users\peted\OneDrive\Documents\PlatformIO\Swarm\Tzatziki/src/mysensors.h:102: multiple definition of `PulseSensor1'; .pio\build\um_feathers3\src\initialize.cpp.o:C:\Users\peted\OneDrive\Documents\PlatformIO\Swarm\Tzatziki/src/mysensors.h:102: first defined here
c:/users/peted/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio\build\um_feathers3\src\pulseCount.cpp.o:C:\Users\peted\OneDrive\Documents\PlatformIO\Swarm\Tzatziki/src/mysensors.h:110: multiple definition of `AnalogSensor1'; .pio\build\um_feathers3\src\initialize.cpp.o:C:\Users\peted\OneDrive\Documents\PlatformIO\Swarm\Tzatziki/src/mysensors.h:110: first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\um_feathers3\firmware.elf] Error 1
It seems like mysensors.h is being run through more than once.
I of course have guards on mysensors.h
Is that last excerpt in one of your header files? You are going to have multiple definitions of PulseSensor1, PulseSensor2, and AnalogSensor1 if you #include that header into multiple, separate .cpp files.
If you need global variables like that, you declare it as extern in the header, and define it in exactly one compilation unit (one .cpp flie).
Well, another half hours and I nailed it... seems like I had already eliminated other #include mysensor.h statements, but I missed one and I moved two of the constructions to another file. So frustrating...
Ganado,
Thank you. So... as I move along here, and I want to use, in multiple places in my code, my class methods defined in mysensor.h which are tied to creation of the object such as PulseSensor1 and AnalogSensor1 (shown above), (such as AnalogSensor1.ClearData(), how can I keep them accessible in multiple places in my code without #includes in the .cpp files that need to access them? I have tried putting the creation of the objects in other places in the code, and that causes other problems. How can I treat the objects like functions (that are made globally available by listing them in a header file, so I end up with them more globally available?
Thanks!
Look at the first example that has file3.h, file1.c, and file2.c.
The example uses a global int variable, but the same pattern applies to any type/class.
#include "AnalogSensor.hpp"
// Defined in exactly one .cpp file; not in a header
AnalogSensor<2048> AnalogSensor1;
void use_AnalogSensor1()
{
AnalogSensor1.ClearData();
}
main.cpp
1 2 3 4 5 6 7 8
#include "AnalogSensor.hpp"
int main()
{
use_AnalogSensor1();
AnalogSensor1.SomethingElse();
}
Thank you Ganado. I of course have used extern for variables but had no idea that it could be used for a class (my mind tends to think of classes as a bunch of functions (methods) rather than a bunch of data).