Search code examples
arduinolinkerlinker-errorsesp32

Arduino, C, Linker error, ESP32 target


I use Arduino IDE to develop code for ESP8266 and ESP32 (that in fact doesn't matter, it's only the toolchain that's different than classic Arduino for Atmel AVRs).

I've had a code that worked, had no build errors or warnings. The project included custom .c/.h files for OneWire, DS18B20 etc.

All okay. Then I added new, custom .c/.h sources, include it in main.ino but get a linker error:

sketch\my_esp32.ino.cpp.o:(.literal._Z14Equalizer_Initv+0x18): undefined reference to `init_3band_state(EQSTATE*, int, int, int)'
sketch\my_esp32.ino.cpp.o: In function `Equalizer_Init()'

Everything is included and I can browse symbols though.. When I use the VisualMicro addon for VisualStudio, it does the same, but shows verbose output that says it has built sources, but failed after Linking it all together...

The problem is in dsp.h/dsp.c

Simplified sources:

my_esp32.ino

#include "dsp.h"

EQSTATE *equalizer;

void Equalizer_Init()
{
    init_3band_state(equalizer, 800, 5000, 50000);
}

void setup()
{
    Equalizer_Init();
}

void loop()
{

}

dsp.h

typedef struct
{
    //some variables here...
}EQSTATE;

void   init_3band_state(EQSTATE* es, int lowfreq, int highfreq, int mixfreq);

dsp.c

#include "dsp.h"

void init_3band_state(EQSTATE* es, int lowfreq, int highfreq, int mixfreq)
{
    //some simple code
}

In output it even says my_esp32.ino:384: undefined reference to init_3band_state(EQSTATE*, int, int, int) but on the line 384 is not code related to this dsp at all.. If I comment out the call to init_3band_state, it will build again.

I'll appreciate any help


Solution

  • I found that there was missing

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // code...
    
    #ifdef __cplusplus
    }
    #endif
    

    in my .h source, maybe one day it'll help to someone with similar problem.