Undefined reference error on library access

Hi,

I wrote a library to make getting the core frequency of a signal (represented by a uint8_t array) easier. The whole thing has a modified version of the arduinoFFT- library as its backbone. I put the required files for the arduinoFFT into a folder in my library (utility) so I don’t have problems with non-existing libs.

Problem now is that I get an error when trying to access the arduinoFFT functions.

Short form:

Folder structure:
FundamentalFrequencyAnalyzer
. FundamentalFrequencyAnalyzer.h
. FundamentalFrequencyAnalyzer.cpp
. keywords.txt
. Examples (empty)
. utility
. . arduinoFFT.h
. . arduinoFFT.cpp
. . defs.h
. . types.h

Include (in FFA.cpp):

#include "utility/arduinoFFT.h"

Short snippet of error message (DCRemoval is a working method in the lib):
undefined reference to `arduinoFFT::DCRemoval()

Long form:

Header:

#ifndef FundamentalFrequencyAnalyzer_h
#define FundamentalFrequencyAnalyzer_h

#include "Arduino.h"

#define WIN_TYP_RECTANGLE FFT_WIN_TYP_RECTANGLE
#define WIN_TYP_HAMMING FFT_WIN_TYP_HAMMING
#define WIN_TYP_HANN FFT_WIN_TYP_HANN
#define WIN_TYP_TRIANGLE FFT_WIN_TYP_TRIANGLE
#define WIN_TYP_NUTTALL FFT_WIN_TYP_NUTTALL
#define WIN_TYP_BLACKMAN FFT_WIN_TYP_NUTTALL
#define WIN_TYP_BLACKMAN_NUTTALL FFT_WIN_TYP_BLACKMAN_NUTTALL
#define WIN_TYP_BLACKMAN_HARRIS FFT_WIN_TYP_BLACKMAN_HARRIS
#define WIN_TYP_FLT_TOP FFT_WIN_TYP_FLT_TOP
#define WIN_TYP_WELCH FFT_WIN_TYP_WELCH

class FundamentalFrequencyAnalyzer {
  public:
    FundamentalFrequencyAnalyzer(uint8_t windowType);
    FundamentalFrequencyAnalyzer();
    uint16_t getFundamentalFrequency(uint8_t *samples, uint16_t samplingFrequency);
  private:
    uint8_t _windowType;
};

#endif

Source:

#include "Arduino.h"
#include "utility/arduinoFFT.h"
#include "FundamentalFrequencyAnalyzer.h"

FundamentalFrequencyAnalyzer::FundamentalFrequencyAnalyzer(uint8_t windowType)
{
  _windowType = windowType;
}

FundamentalFrequencyAnalyzer::FundamentalFrequencyAnalyzer()
{
  _windowType = WIN_TYP_RECTANGLE;
}

uint16_t FundamentalFrequencyAnalyzer::getFundamentalFrequency(uint8_t *samples, uint16_t samplingFrequency)
{
  const uint16_t numSamples = sizeof(samples);
  uint8_t vReal[numSamples];
  memcpy(vReal, samples, numSamples);
  uint8_t vImag[numSamples];
  memset(vImag, 0, numSamples);
  arduinoFFT FFT(vReal, vImag, numSamples, samplingFrequency); // create FFT object
  FFT.DCRemoval();
  FFT.Windowing(_windowType, FFT_FORWARD);
  FFT.Compute(FFT_FORWARD);
  FFT.ComplexToMagnitude();
  return (uint16_t)FFT.MajorPeak();
}

Test prog:

#include "FundamentalFrequencyAnalyzer.h";

void setup(){
  Serial.begin(115200);
  FundamentalFrequencyAnalyzer ffa = FundamentalFrequencyAnalyzer();
  uint8_t samples[128];
  boolean b = 0;
  for(uint16_t i = 0; i < 32; i++){
    for(uint16_t j = 0; j < 4; j++){
      samples[i*4+j] = b;
    }
    b ^= 1;
  }
  Serial.println(ffa.getFundamentalFrequency(samples, 1000));
}

void loop(){
  
}

Error message:

Arduino: 1.8.10 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Daniel\AppData\Local\Temp\cc1jC6Hz.ltrans0.ltrans.o: In function `getFundamentalFrequency':

sketch/FundamentalFrequencyAnalyzer.cpp:22: undefined reference to `arduinoFFT::arduinoFFT(unsigned char*, unsigned char*, unsigned int, unsigned int)'

sketch/FundamentalFrequencyAnalyzer.cpp:23: undefined reference to `arduinoFFT::DCRemoval()'

sketch/FundamentalFrequencyAnalyzer.cpp:24: undefined reference to `arduinoFFT::Windowing(unsigned char, unsigned char)'

sketch/FundamentalFrequencyAnalyzer.cpp:25: undefined reference to `arduinoFFT::Compute(unsigned char)'

sketch/FundamentalFrequencyAnalyzer.cpp:26: undefined reference to `arduinoFFT::ComplexToMagnitude()'

sketch/FundamentalFrequencyAnalyzer.cpp:27: undefined reference to `arduinoFFT::MajorPeak()'

sketch/FundamentalFrequencyAnalyzer.cpp:22: undefined reference to `arduinoFFT::~arduinoFFT()'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

UPDATE: It actually is the include, I have included the arduinoFFT as a .zip library and all was fine

It won’t work as written. This:

const uint16_t numSamples = sizeof(samples);

returns the size of the pointer, not the size of the data array it’s pointing to.

I just found the solution to what I now know was my problem:
Arduino only accepts imports from the sketch folder if the subfolder is called 'src'

See: How to include from subfolder of sketch folder - Programming Questions - Arduino Forum

Thanks for your help!

PS:

gfvalvo:
It won't work as written. This:

const uint16_t numSamples = sizeof(samples);

returns the size of the pointer, not the size of the data array it's pointing to.

Thanks, corrected that