SameFile: Multiple Definition of variable; SameFile: first defined here (Linker ignores include guards?)
Using:
- PlatformIO Core, version 6.1.19
- CLion 2024.3.1.1
- Windows 10 Pro 22H2 (OS Build 19045.6466)
- MinGW 11.0 w64
- CMake 3.29.2
When trying to use multiple files with what i assume to be proper include guards.
My intuition tells me that each file is compiled without the others guard define included, since when i include a #ifndef LINKING_ERROR_MAIN_H guard manually to the aVariable it compiles fine, so how should i handle this here?
main.h
#ifndef LINKING_ERROR_MAIN_H
#define LINKING_ERROR_MAIN_H
#include <Arduino.h>
#include "main2.h"
void setup();
void loop();
#endif //LINKING_ERROR_MAIN_H
main.cpp
#include "main.h"
void setup() {
// write your initialization code here
}
void loop() {
// write your code here
}
main2.h
#ifndef LINKING_ERROR_MAIN2_H
#define LINKING_ERROR_MAIN2_H
class main2 {
};
void aFunction();
//#ifndef LINKING_ERROR_MAIN_H
bool aVariable;
//#endif
#endif //LINKING_ERROR_MAIN2_H
main2.cpp
#include "main2.h"
platform.ini
[env:heltec_wifi_lora_32_V3]
platform = espressif32
board = heltec_wifi_lora_32_V3
framework = arduino
Build CMD output:
C:\Users\Syra\.platformio\penv\Scripts\pio.exe run -e heltec_wifi_lora_32_V3
Processing heltec_wifi_lora_32_V3 (platform: espressif32; board: heltec_wifi_lora_32_V3; framework: arduino)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/heltec_wifi_lora_32_V3.html
PLATFORM: Espressif 32 (6.12.0) > Heltec WiFi LoRa 32 (V3)
HARDWARE: ESP32S3 240MHz, 320KB RAM, 8MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-builtin, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
- framework-arduinoespressif32 @ 3.20017.241212+sha.dcc1105b
- tool-esptoolpy @ 2.40900.250804 (4.9.0)
- toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
- toolchain-xtensa-esp32s3 @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 33 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\heltec_wifi_lora_32_V3\src\main.cpp.o
Compiling .pio\build\heltec_wifi_lora_32_V3\src\main2.cpp.o
Linking .pio\build\heltec_wifi_lora_32_V3\firmware.elf
c:/users/syra/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio\build\heltec_wifi_lora_32_V3\src\main2.cpp.o:C:\Users\Syra\CLionProjects\linking_error/src/main2.h:15: multiple definition of `aVariable'; .pio\build\heltec_wifi_lora_32_V3\src\main.cpp.o:C:\Users\Syra\CLionProjects\linking_error/src/main2.h:15: first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\heltec_wifi_lora_32_V3\firmware.elf] Error 1
========================== [FAILED] Took 8.58 seconds ==========================
you don't need to include main2.h in main.h, except you need to call a function or access a varibale thats declared in main2.h
Just include it in main.cpp.
You also don't need the empty class main2
You can use #pragma once as iclude guard without the #ifndef, #define and #endif.
Since all cpp files are compiled, no matter if the header is included, you could also just remove main.h. It would work exactly the same.
I would recommend, if you need to split that into header and cpp files (which is not necessary), to do it like that:
main.h:
#pragma once
#include <arduino.h>
void loop();
void setup();
main.cpp:
#include "main.h"
#include "main2.h"
void setup(){
//Do something
aVariable=true; //Access the variable declared in main2.h
}
void loop(){
// Do something
aFunction(); //You need to call your function somewhere otherwise the linker will strip it out
}
main2.h:
#pragma once
void aFunction();
bool aVariable;
main2.cpp:
#include "main2.cpp"
void aFunction(){
//Do something
}
But as far as I know, that won't solve your problem.
I think the problem ist, that loop() and setup() are already declared in arduino.h.
Therefore you get the linker error, because they are declared multiple times.
Just remove void setup() and void loop() from your main.h and you should be fine.
Ok, i figured it out.
Basically what im trying to do is just subdivide my files in multiple files, with them sharing some variables, cause functions dont seem to be the problem (the error message explicitly states aVariable to be the problem, and removing void setup() and void loop() from main.h didnt solve the problem).
The ONLY way i got it to work was with this setup.
I also got it to work without any .h files but that would mean copying over any functions and variables i want to use in any other .cpp over from the other .cpp so yeah thats not happening.
This works i guess but this also means that you have to add extern to every variable other than const variables in the header, which also means you cant assign them/that has to be done in the .cpp.
I will leave this open for a few days just so more people can see it/possible can give suggestions to make this cleaner (or devs can close it thats also fine) but since i "technically" found a solution i will close it than.
main.cpp
#include <arduino.h>
#include "main2.h"
void setup() {
aFunction();
Serial.begin(115200);
Serial.println(aVariable ? "True" : "False");//properly prints "True"
Serial.printf("%i",CONST_INT);//properly prints "1"
}
void loop() {
}
main2.cpp
#include "main2.h"
bool aVariable = false;
void aFunction(){
aVariable = true;
}
main2.h
#pragma once
const int CONST_INT = 1;// is technically defined twice but works fine for some reason
void aFunction();
extern bool aVariable;
main.h removed
Please forward to https://community.platformio.org/