Search code examples
c++timearduinoinclude

C++ , 'time' was not declared in this scope, without reason


I'm building an Arduino sketch and I'm stuck with a strange error:

'time' was not declared in this scope

These are my includes:

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <time.h>
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
#include <TimeLib.h>

And this is the function where I get the error:

void wifiTimeSetup() {
  configTime(timezone * 3600, 0, "pool.ntp.org", "time.nist.gov");
  Serial.println("Mi sto collegando al servizio per l'ora esatta tramite internet");
  while (!time(nullptr)) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("");
}

The error comes up when including the Time.h and TimeLib.h libraries, but I need them too, to use my RTC module.


Solution

  • Including both time.h and Time.h is generally not a good idea.

    This is due to case sensitivity or insensitivity of the compiler your'e working with.

    Apparently this conflict was already posted as a known issue:

    https://github.com/PaulStoffregen/Time/issues/74

    Some Arduino users are trying to use this library with libraries like RTCZero that #include on case sensitive filesystems like macOS and older Windows. Here's a related issue: arduino-libraries/RTCZero#28 The compiler ends up including Time.h instead of the POSIX time.h inside the RTCZero library which causes compile errors. Would you be open to renaming Time.h in the library to avoid conflicts?