Linker error: multiple definition

Hi there,
I’m trying to start develop with Platformio and am having hard time with linker errors. I’m experienced with Objective-C and partially Swift but haven’t spend time with C++. I started with basic Arduino OTA example and wanted to add an ESP WiFiManager code. To make code clean I created a OTACode.cpp/h and moved all OTA specific code there and wanted to do the same with WFCode.cpp/h. Unfortunately I’m unable to compile since I did it, getting tons of linker errors. I added a pragma once directive to the header file, but it didn’t help. Could anyone give me a hand?

/Users/rcerny/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: .pio/build/nodemcuv2_ota/src/main.cpp.o: in function `_ZN15ESP_WMParameterC2EPKc':
main.cpp:(.text._ZN15ESP_WMParameterC2EPKc+0x0): multiple definition of `_ZN15ESP_WMParameterC2EPKc'; .pio/build/nodemcuv2_ota/src/WFCode.cpp.o:WFCode.cpp:(.text._ZN15ESP_WMParameterC2EPKc+0x0): first defined here
/Users/rcerny/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: .pio/build/nodemcuv2_ota/src/main.cpp.o: in function `_ZN15ESP_WMParameterC2EPKc':
main.cpp:(.text._ZN15ESP_WMParameterC2EPKc+0x0): multiple definition of `_ZN15ESP_WMParameterC1EPKc'; .pio/build/nodemcuv2_ota/src/WFCode.cpp.o:WFCode.cpp:(.text._ZN15ESP_WMParameterC2EPKc+0x0): first defined here

main.cpp

#include <Arduino.h>

#include "OTACode.h"
#include "WFCode.h"


void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  
  WFsetup();
  OTAStart();

}

WFCode.h

#pragma once

....a lot of defines here....
void WFsetup();
void check_status();

I found some post about adding inline directive to functions, but the result is the same

Thanks a lot!
Robert

The demangled name for this is

ESP_WMParameter::ESP_WMParameter(char const*)

You have to take care to not ever, ever ever define functions in a header file, only to declare them. When you define a function in a header file that is included by multiple .c/.cpp files (and the error message clearly tells you that’s happening in src/main.cpp and src/WFCode.cpp), the function code will be present in all the object files resulting from these source files (main.o and WFCode.o) and the linker will then throw an error that the function was defined in multiple object files.

//.h file
#include <header_for_types_you_need.h>
// only declaration
ESP_WMParameter::ESP_WMParameter(char const*);
//.cpp file
#include <header_file_above.h>
ESP_WMParameter::ESP_WMParameter(char const*) {
   /* actual function code only in .cpp file */
}

This is an extremely common beginner’s C/C++ programmer mistake and there’s at least one post per week about this type of mistake, that is. Together with the misunderstanding of the effect of #pragma once. See similiar topics like Struggling on #include directive.

Hah. Actually, judging from the class name, you seem to be using

in which case, you did not make that mistake, but the author of the libary did.

Welp, the only options are to fix their mistakes, only ever include that library’s header file in one .cpp file as a workaround, or use a different library. Many such cases.

This case especially, the author explicitly states this problem as WONTFIX.

Max,
thanks a lot for your time spent on this detailed explanation.