PlatformIO Community

Arduino Code in PlatformIO Will Not Compile for Unknown Reason

Hi, I am working on a project that runs on an esp32 dev-kit C. My code compiles in Arduino IDE but when I ported it into platformIO on VSCode, it does not compile. Initially, this was because I needed to add #include files to each .cpp file and bring all the function declarations or variables to the top of the file or to a separate header file. After going through this long process, there were no more errors in my files but the .elf file failed as did the build. I do not know why and have been struggling with this for a while, here’s my terminal output:

c:/users/matth/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio/build/esp32doit-devkit-v1/src/main.cpp.o:C:\Users\matth\Documents\PlatformIO\Projects\MVP_Code/src/00_main.h:48: multiple definition of `access_point_mode'; .pio/build/esp32doit-devkit-v1/src/01_Async_Server.cpp.o:C:\Users\matth\Documents\PlatformIO\Projects\MVP_Code/src/00_main.h:48: first defined here
c:/users/matth/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio/build/esp32doit-devkit-v1/src/main.cpp.o:C:\Users\matth\Documents\PlatformIO\Projects\MVP_Code/src/00_main.h:46: multiple definition of `user_PW'; .pio/build/esp32doit-devkit-v1/src/01_Async_Server.cpp.o:C:\Users\matth\Documents\PlatformIO\Projects\MVP_Code/src/00_main.h:46: first defined here
c:/users/matth/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio/build/esp32doit-devkit-v1/src/main.cpp.o:C:\Users\matth\Documents\PlatformIO\Projects\MVP_Code/src/00_main.h:45: multiple definition of `user_SSID'; .pio/build/esp32doit-devkit-v1/src/01_Async_Server.cpp.o:C:\Users\matth\Documents\PlatformIO\Projects\MVP_Code/src/00_main.h:45: first defined here
c:/users/matth/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio/build/esp32doit-devkit-v1/src/Drying_RTOS.cpp.o:(.literal._Z30Drying_Condenser_Sequence_RTOSv+0x0): undefined reference to `Drying_Condenser_Sequence_RTOS_is_ON'
c:/users/matth/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio/build/esp32doit-devkit-v1/src/Mixing_Motor.cpp.o:(.literal._Z19Timed_Transfer_TaskPv+0x10): undefined reference to `Timed_Transfer_is_ON'
**collect2.exe: error: ld returned 1 exit status**
***** [.pio\build\esp32doit-devkit-v1\firmware.elf] Error 1**```
(The terminal output is a lot longer but I could not fit it within the character limit and it was pretty similar to the warnings noted above)
In summary, all these outputs are warnings, not errors. It is only the last 2 lines of the terminal that are causing me problems and I don't know why? Can someone please help? Also unfortunately, I don't have permission to share the code.

This error would occur if you write a global variable definition in your header like

#ifndef _MAIN_H_
#define _MAIN_H_

const char user_PW[] = "ABC";

#endif

If multiple .c/.cpp files include this header, each of them will attempt the global variable, however, there can only be one instance of it. The correct way is to use an extern declaration in the header, and a definition of the variable in one .cpp file. This has been discussed multiple times in topics like e.g. Splitting cpp files - #2 by maxgerhardt.

In which file do you define this variable?

@maxgerhardt Thank you for your quick reply, sorry I didn’t see this notification earlier. I declare the boolean inside the header file Drying_RTOS.h like so:
extern bool Drying_Condenser_Sequence_RTOS_is_ON;
and in Drying_RTOS.cpp, in one of the functions, I define it as being true. I also reference it at other places in my code to check if it’s true or false inside functions. Would I need to declare it as an extern in every file at the very top where it’s being used or can I just reference it as a normal global variable inside my other files in their respective functions? Also, I don’t know how to use extern variables with structs. For example, this is how I’m currently doing it:
In PWM.h:

struct PWM {
  String name = "FOO";
  int freq = DEFAULT;
  int channel;
  //other members of the struct
};

extern PWM motor;             //Drying Heater
extern PWM motor2;                //Air Pump

And then inside PWM.cpp, I have this:

#include "PWM.h"

PWM motor;             
PWM motor2;         

And then inside other .cpp files elsewhere in my codebase, I turn on motors (in motor.cpp) using functions like MotorOn(&motor2);
In motor.cpp, I also add the following at the top of the file: #include "PWM.h"
How can I fix this struct declaration and definitions along with anything else you believe might be causing me errors?

Can you post the full code in a repistory? I’m having a hard time going only on the snippets.