This issue eludes me … once again. Undefined reference to a class object.
It is similar to one we looked at earlier, but the answer does not seem to be similar to the prior solution…
https://cplusplus.com/forum/general/284834/
I apologize for my denseness on this matter.
I have created a class to manage an ADC converter chip. That class is declared in ADS1216.h
ADS1216.h
1 2 3 4 5 6 7 8 9 10
|
class ADS1216_PD
{
public:
// Constructors
ADS1216_PD(int cs, int drdy);
ADS1216_PD(SPIClass *s, int cs, int drdy);
...
void setMux(ADS1216_MUX muxSetting);
};
| |
The functions are defined in ADS1216.cpp:
ADS1216.cpp
1 2 3 4 5 6 7 8
|
/* Constructors */
ADS1216_PD::ADS1216_PD(int cs, int drdy) : _spi{&SPI}, csPin{cs}, drdyPin{drdy} {}
ADS1216_PD::ADS1216_PD(SPIClass *s, int cs, int drdy) : _spi{s}, csPin{cs}, drdyPin{drdy} {}
...
void ADS1216_PD::setMux(ADS1216_MUX muxSetting)
{
writeRegister(ADS1216_MUX_REG, muxSetting);
}
| |
In main(), which lives in main.cpp, I call initialize(), which lives in initialize.cpp and initialize() creates the myADC object.
initialize.cpp
1 2 3 4 5 6
|
#include "SPI.h"
void initialize(void)
{
ADS1216_PD myADC = ADS1216_PD(PIN_ADC_CS_BAR, PIN_ADC_DRDY_BAR);
}
| |
(Also in initialize I read and write some of the ADC registers, using other class functions not shown here, to confirm it is working)
Now back in main(), after initialize completes, I go to call one of the myADC object functions.
main.cpp
1 2 3 4 5 6 7 8 9 10
|
#include <ADS1216.h>
extern ADS1216_PD myADC;
void setup()
{
initialize();
myADC.setMux(ADS1216_MUX_AIN3_AINCOM);
}
| |
(Of course lots of other code is not shown here…) But it compiles fine and at link time throws this error:
1 2 3 4
|
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/esp32-s3-devkitc-1/src/main.cpp.o:(.literal._Z5setupv+0x0):
undefined reference to `myADC'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp32-s3-devkitc-1\firmware.elf] Error 1
| |
Commenting out the setMux call in main() and it compiles and runs fine.
What is odd is that this structure seems identical to another program I have linking just fine which used the ADS1220_WE library defining a similar object the same way. What am I missing here? (I have tried extern statements in several places to no avail). THANK YOU.