Multiple definition error

I’m stumped. I’ve got a library, and it keeps giving me multiple definition errors when I try to compile, but I can’t see what the problem is. Hope someone can point me in the right direction. There are no other versions of this library in my project or in my library folder and no where else I can think of that the compiler might be finding other definitions.

Thanks.

Errors:

Compiling debug version of 'Graphics_v03' for 'Arduino Nano Every'
 
thirdOctave.cpp.o (symbol from plugin)*: In function returnOctaveBands(double*, double*, short, double, double)
(.text+0x0)*: multiple definition of firstBinAR

Error linking for board Arduino Nano Every
Graphics_v03.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
Debug build failed for project 'Graphics_v03'
 
thirdOctave.cpp.o (symbol from plugin)*: In function returnOctaveBands(double*, double*, short, double, double)
(.text+0x0)*: multiple definition of gainIndexAR
Graphics_v03.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
 
thirdOctave.cpp.o (symbol from plugin)*: In function returnOctaveBands(double*, double*, short, double, double)
(.text+0x0)*: multiple definition of binCountAR
Graphics_v03.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
 
thirdOctave.cpp.o (symbol from plugin)*: In function returnOctaveBands(double*, double*, short, double, double)
(.text+0x0)*: multiple definition of gainTermsAR
Graphics_v03.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
 
thirdOctave.cpp.o (symbol from plugin)*: In function returnOctaveBands(double*, double*, short, double, double)
(.text+0x0)*: multiple definition of centerFrequencyAR
Graphics_v03.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
 
collect2.exe*: error: ld returned 1 exit status

thirdOctave.h:

#ifndef _thirdOctave_h
#define _thirdOctave_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "arduino.h"
#else
	#include "WProgram.h"
#endif

#define MAX_OCTAVE  32
#define BIN_COUNT   64

short centerFrequencyAR[33] = {
	0,         16,        20,        25,        32,        40,        50,        63,        79,       100,
	126,       158,       200,       251,       316,       398,       501,       631,       794,      1000,
	1259,      1585,      1995,      2512,      3162,      3981,      5012,      6310,      7943,     10000,
	12589,     15849,     19953
};

float gainTermsAR[113] = {
	0.0000,    0.6867,    0.2404,    0.9925,    0.2704,    0.9845,    0.6122,    0.7097,    0.9980,    0.2313,
	0.2017,    0.9942,    0.9797,    0.2599,    0.2840,    0.9877,    0.9995,    0.5896,    0.1574,    0.1769,
	0.7324,    0.9999,    0.9973,    0.6581,    0.2227,    0.2093,    0.6636,    0.9956,    1.0000,    0.9751,
	0.5989,    0.2499,    0.1204,    0.1232,    0.2956,    0.7230,    0.9903,    1.0000,    0.9992,    0.9239,
	0.5673,    0.2813,    0.1521,    0.1834,    0.3820,    0.7547,    0.9807,    0.9999,    1.0000,    0.9963,
	0.9160,    0.6351,    0.3648,    0.2144,    0.1354,    0.1268,    0.2173,    0.3925,    0.6867,    0.9363,
	0.9967,    1.0000,    1.0000,    0.9986,    0.9696,    0.8258,    0.5765,    0.3692,    0.2404,    0.1638,
	0.1166,    0.1272,    0.1944,    0.3078,    0.4950,    0.7455,    0.9332,    0.9925,    0.9998,    1.0000,
	1.0000,    0.9988,    0.9837,    0.9116,    0.7467,    0.5455,    0.3823,    0.2704,    0.1965,    0.1470,
	0.1130,    0.1356,    0.1902,    0.2729,    0.3982,    0.5753,    0.7764,    0.9238,    0.9845,    0.9984,
	1.0000,    1.0000,    1.0000,    0.9996,    0.9951,    0.9720,    0.9029,    0.7731,    0.6122,    0.4647,
	0.3501,    0.2665,    0.2065
};

short binCountAR[33] = {
	0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	2,     2,     2,     3,     4,     5,     6,     8,    10,    12,
	16,    20,    22
};

byte gainIndexAR[33] = {
	0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	1,     3,     5,     7,    10,    14,    19,    25,    33,    43,
	55,    71,    91
};

short firstBinAR[33] = {
	0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
	3,     4,     5,     6,     7,     9,    11,    14,    17,    22,
	27,    34,    43
};

short returnOctaveBands(double fftData[], double bandData[], short bins, double k, double vRef);

#endif

thirdOctave.cpp:

#include "thirdOctave.h"

short returnOctaveBands(double fftData[], double bandData[], short bins, double k, double vRef) {
	Serial.println("Hi");
}

Please post the Graphics_v03 sketch.

Quercus47:

Compiling debug version of 'Graphics_v03' for 'Arduino Nano Every'

Which Arduino development software (e.g., Arduino IDE, Arduino CLI, Arduino Pro IDE, Arduino Web
Editor, PlatformIO) are you using? I don’t recall seeing this “Compiling debug version of …” message before.

As already stated, we can't know for sure unless you post all your code. But, I'm going to guess ... I'll bet that your main sketch file has a line that says:

#include "thirdOctave.h"

Once you do that, you've defined all those arrays twice (once in thirdOctave.cpp and once in the sketch) . That's why the linker is complaining about "multiple definition". It's correct to do so.

Variable definitions don't belong in .h files. Only 'extern' declarations of variables belong there. You can have multiple declarations, but only one definition of each variable. See https://www.cprogramming.com/declare_vs_define.html.

Also, see my Reply #3 here for a description of a correct way to break a project into multiple .h and .cpp files.

I’ll move the declarations and see what happens, thanks.

pert: Which Arduino development software ? I don't recall seeing this "Compiling debug version of ..." message before.

I'm using Visual Studio and liking it, although I got the same error when trying the Arduino IDE.

gfvalvo: Variable definitions don't belong in .h files. Only 'extern' declarations of variables belong there.

That was it, thanks. I moved the array definitions into the cpp file and the error went away. BTW, when I took out the #include "thirdOctave"; line, it took out the reference and the compiler complained that returnOctaveBands was undefined. But the code is compiling now, thank you.

Quercus47: That was it, thanks. I moved the array definitions into the cpp file and the error went away. BTW, when I took out the #include "thirdOctave"; line, it took out the reference and the compiler complained that returnOctaveBands was undefined. But the code is compiling now, thank you.

Post your current code.