Collect2.exe: error: ld returned 1 exit status *** [.pio\build\d1\firmware.elf] Error 1

Hello I am doing an application on ESP8266 Wemos d1 r1 using Platformio. When I compile, I get the following error message.

When I compile the same code with Arduino ide, it does not give an error.
What could be the mistake.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"

/*===========================================================================
                       GPIO PINS
===========================================================================*/
const int relayPin1 = 13; // Living Room One
const int relayPin2 = 12; // 
const int relayPin3 = 14; // 
const int relayPin4 = 16; // LivingRoomTv
/*===========================================================================
                        WIFI SSID AND PASSWORD
===========================================================================*/

const char* ssid = "xxxxxxxx";           //type your ssid
const char* password = "xxxxx";                //type your password
/*============================================================================
                        prototypes
===========================================================================*/
boolean connectWifi();
bool officeLightsOn();
bool officeLightsOff();
bool kitchenLightsOn();
bool kitchenLightsOff();

boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;

/*=========================================================================
                        SETUP
===========================================================================*/
void setup()
{
  
}

/*=======================================================================
                        LOOP
========================================================================*/
void loop()
{
  // put your main code here, to run repeatedly:
}

Error Message:

Scanning dependencies...
Dependency Graph
|-- <ESP8266WebServer> 1.0
|   |-- <ESP8266WiFi> 1.0
|-- <ESP8266WiFi> 1.0
Building in release mode
Linking .pio\build\d1\firmware.elf
c:/users/hikmet/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\d1\src\main.cpp.o:(.text.startup._GLOBAL__sub_I_ssid+0x8): undefined reference to `UpnpBroadcastResponder::UpnpBroadcastResponder()'
c:/users/hikmet/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\d1\src\main.cpp.o: in function `_GLOBAL__sub_I_ssid':
main.cpp:(.text.startup._GLOBAL__sub_I_ssid+0x15): undefined reference to `UpnpBroadcastResponder::UpnpBroadcastResponder()'
c:/users/hikmet/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\d1\src\main.cpp.o:(.text.exit._GLOBAL__sub_D_ssid+0x0): undefined reference to `UpnpBroadcastResponder::~UpnpBroadcastResponder()'
c:/users/hikmet/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\d1\src\main.cpp.o: in function `_GLOBAL__sub_D_ssid':
main.cpp:(.text.exit._GLOBAL__sub_D_ssid+0x13): undefined reference to `UpnpBroadcastResponder::~UpnpBroadcastResponder()'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\d1\firmware.elf] Error 1

Can you provide more information about your project, in particular:

  • What does your platformio.ini look like?
  • What does the file and directory organization look look like? Where are switch.h, UpnpBraodcastResponder.h, and CallbackFunction.h?

And please take care to properly format your code. You have to select the text that you want to format before you click the Preformatted text or Blockquote icon. I’ve taken the liberty to format your question.

platformio.ini
[env:d1]
platform = espressif8266
board = d1
framework = arduino

Project Tree

Thank you so much. Respects.

Move the files Switch.cpp and UpnpBroadcastResponder.cpp to the src folder. Then it should work – or you should at least make progress.

include and src belong togehter. lib should only contain directories with further subdirectories.

There is no compilation problem when moving .cpp files to the scr folder.
Thank you for your help and guidance.

In the Include folder, we move files with .h extension and .cpp extension to the scr folder.
So how do we use the lib folder structure. Can you show a simple example?

Best regards.

The best approach is NOT to use the lib folder. If you need third party libraries, declare them in platformio.ini and PlatformIO will take care of the rest.

The lib folder is needed if you want to use a local copy of a third-party library and modify it for your particular project. Then put the library (or several libraries) into lib, each with its own directory.