HiI’m do the efforts to write my very own code and also I should write some functione to different .cpp and .h files. So my sketch is made by the main file (a .ino one, the web concept created it for me once I select to begin a new project) with an incorporate for the new .h record with the statements of functions and also variables ns need. The same include is within the .cpp record that contains the attributes definitions.Now the problem: when I shot to verify mine code, the compiler say the all the variable are dual defined and abort.I shot to usage #ifndef / #define / #endif declaration to have a solitary inclusion that .h file, yet it doesn’t solve the problem.

You are watching: Multiple definition of function c++

#playground.ino: #include "lmt871.h" ... My code ...#lmt871.cpp: #include "lmt871.h" ... My password ...#lmt871.h: #ifndef LMT_H #define LMT_H ... Variable and role declarations ... #endifAnyone can aid me?Thanks

P.S. Ns read about that #include “application.h” thing, but including it top top every records doesn’t settle the problem.


mdma April 19, 2015, 4:31pm #2

Without seeing the password or the precise error message, we can only sell guesses, therefore please write-up those.

*

If you’re specifying functions in the header file, it’s feasible to obtain a linker error since the same function is then defined in an ext than one module. Be sure to only put the declaration in the header document and then the duty body in the .cpp file.


Paciotti April 19, 2015, 4:40pm #3

Sorry, this is the error:

In file included native ../inc/spark_wiring.h:29:0,from ../inc/application.h:29,from lmt871.cpp:1:../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to relax Build" <-Wcpp>#warning "Defaulting to release Build"^In paper included native ../inc/spark_wiring.h:29:0,from ../inc/application.h:29,from playground.cpp:2:../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to relax Build" <-Wcpp>#warning "Defaulting to release Build"^playground.o:(.bss+0x10): multiple meaning of `mVinf"lmt871.o:(.bss+0x4): very first defined hereplayground.o:(.bss+0x14): multiple definition of `mVsup"lmt871.o:(.bss+0x0): very first defined hereplayground.o:(.data+0x1c): multiple an interpretation of `mV"lmt871.o:(.data+0x4): first defined hereplayground.o:(.data+0x110): multiple definition of `arrayDim"lmt871.o:(.data+0x0): very first defined herecollect2: error: ld reverted 1 exit statusmake: *** <86478ad9583bbedc1252ae91d73a2f6cb278580cf813a1e653d4d6ffe1d0.elf> Error 1That one is the .h code:

#include "application.h"#ifndef LMT871_H#define LMT871_Hint arrayDim = 61;int mV<>= 2767,2754,2740,2727,2714,2700,2687,2674, 2660,2647,2633,2620,2607,2593,2580,2567, 2553,2540,2527,2513,2500,2486,2473,2459, 2446,2433,2419,2406,2392,2379,2365,2352, 2338,2325,2311,2298,2285,2271,2258,2244, 2231,2217,2204,2190,2176,2163,2149,2136, 2122,2108,2095,2081,2067,2054,2040,2026, 2012,1999,1985,1971,1958;int mVsup, mVinf;double getTemp(int millis);#endifand the .cpp one:

#include "application.h"#include "lmt871.h"double getTemp(int millis){ double temp = 0; because that (int ns = 0; ns playground.ino is usually a tinker firmware with

#include "application.h"#include "lmt871.h"and a contact to getTemp().


*
Paciotti:

int arrayDim = 61;int mV<>=2767,2754,2740,2727,2714,2700,2687,2674,2660,2647,2633,2620,2607,2593,2580,2567,2553,2540,2527,2513,2500,2486,2473,2459,2446,2433,2419,2406,2392,2379,2365,2352,2338,2325,2311,2298,2285,2271,2258,2244,2231,2217,2204,2190,2176,2163,2149,2136,2122,2108,2095,2081,2067,2054,2040,2026,2012,1999,1985,1971,1958;int mVsup, mVinf;double getTemp(int millis);

Thanks. Yep, my suspicions to be correct!

*
it’s a linker error because the exact same symbols space being defined in the cpp modules.

See more: " Gold Rush: White Water Season 2 Episode 9 ), Gold Rush: White Water

You deserve to fix this by moving the declaration to your lmt871.cpp file, and changing the header record to:

const int arrayDim = 61;extern int* mV;extern int mVsup, mVinf;double getTemp(int millis); This tells the compiler that elsewhere there space these variables, and the linker find them in the compiled lmt871.cpp file.


1 like

Sorry, however I didn’t gain it…In lmt871.h paper I declare part variables (and initialise a pair the them) and a function. In lmt871.cpp ns don’t declare anything, ns only specify the role getTemp.I’m no an proficient programmer, yet I supplied to job-related like this: declaration on .h record and an interpretation on .cpp file.In truth if I readjust the password the means you suggest, ns get an additional error:

lmt871.o: In function `getTemp(int)":/spark/compile_service/shared/workspace/2_compile-server2/core-firmware/build/lmt871.cpp:16: undefined reference to `mV"/spark/compile_service/shared/workspace/2_compile-server2/core-firmware/build/lmt871.cpp:16: undefined referral to `mVsup"/spark/compile_service/shared/workspace/2_compile-server2/core-firmware/build/lmt871.cpp:16: undefined recommendation to `mVinf"If I’m no wrong, that make sense since those variables are not extern for the function getTemp. I’m much more inclined to think there is a difficulty with the twin inclusion of lmt871.h…