Does not compile multi-file project in PlatformIO, Arduino framework

Hi folks. When writing the project decided to divide it into files, when adding all the logic that relates to the sensor BME280 on the files, the code stopped compiling. Here is the error text:collect2.exe: error: ld returned 1 exit status *** [.pio\build\upesy_wroom\firmware.elf] Error 1

If all the logic is located in one file, the code compiles and works fine.

Here’s the code by file: main.cpp

#include <Arduino.h>
#include "bme280.h"

TaskHandle_t Task1Handle;
void Task1code(void *pvParameters);


void setup() 
{ 
  Serial.begin(9600);
  
  I2CBME.begin(I2C_SDA, I2C_SCL, 100000);
  
  bool status;
  /// default settings
  // (you can also pass in a Wire library object like &Wire2)
  status = bme.begin(0x76, &I2CBME);  
  if (!status) 
  {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
  
  Serial.println("-- Default Test --");

   xTaskCreatePinnedToCore(
      Task1code,   /* Функція для реалізації задачі*/
      "Task1",     /* Назва задачі */
      1500,        /* Розмір стеку  */
      NULL,        /* Параметр задачі */
      2,           /* Пріорітет задачі */
      &Task1Handle,/* Покажчик на задачу */
      0);          /* Закріплення задачі за ядром */
}


void Task1code(void *pvParameters)
{
 String TempHumidityPressure;
 
  for(;;)
  {
    
    TempHumidityPressure = BME280();

    Serial.println(TempHumidityPressure);

    vTaskDelay(2000 / portTICK_PERIOD_MS);
  }
}

void loop() {}

bme280.h

#ifndef BME280_H_
#define BME280_H_

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define I2C_SDA 17
#define I2C_SCL 5

TwoWire &I2CBME = Wire;
Adafruit_BME280 bme;

String BME280();

#endif

bme280.cpp

#include <Arduino.h>
#include "bme280.h"



String BME280()
{
  float temperature, humidity, pressure;
  String TempHumidityPressure;

  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0F;  // Преобразуем давление в hPa
    
  // Создаем строку с результатами
  TempHumidityPressure = "Temperature:" + String(temperature) + " C, " +
                         "Humidity:" + String(humidity) + " %, " +
                         "Pressure:" + String(pressure) + " hPa";
  
  return TempHumidityPressure;                     
}

platformio.ini

[env:upesy_wroom]
platform = espressif32
board = upesy_wroom
framework = arduino
monitor_speed = 9600
lib_deps = 
    adafruit/Adafruit BME280 Library@^2.2.4  
    adafruit/Adafruit Unified Sensor@^1.1.14

Any idea why the code stopped compiling when I split it into different files? Thank you very much for your time!

What are you doing in that header file.

1 Like

The project uses custom pins SDA,SCL instead of standard ones, for this purpose they are initialized by this line of code in the file bme280.h.

Here is an example of how new bus pins are declared: ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals | Random Nerd Tutorials

That’s not what I meant. You are defining variables in the bme280.h file instead of declaring them with extern. That will lead to a linker failure as I’ve linked above.

// in bme28.h
extern TwoWire &I2CBME;
extern Adafruit_BME280 bme;
// in bme280.cpp
TwoWire &I2CBME = Wire;
Adafruit_BME280 bme;
1 Like

Thank you so much, now I understand what extern is for! Thank you again for your time!