PlatformIO Community

Build error if time library is installed

I am using the following code which builds and runs as expected:

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <time.h>

int timezone = 3 * 3600;
int dst = 0;

void setup() {
  Serial.begin(115200); Serial.println();
  WiFi.waitForConnectResult();
  configTime(timezone, dst, "pool.ntp.org", "time.nist.gov");
  Serial.println("\nWaiting for time");
  while (!time(nullptr)) {
    Serial.print(".");
    delay(1000);
  }
}

void loop() {
  time_t t = time(NULL);
  struct tm* p_tm = localtime(&t);
  Serial.println(ctime(&t));
  Serial.printf("Year: %d Month: %02d Day: %02d Hour: %02d Minute: %02d Sec: %02d wday: %d yday: %d isdst: %d\n\n",
                                                          p_tm->tm_year+1900, p_tm->tm_mon+1, p_tm->tm_mday,
                                                          p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec,
                                                          p_tm->tm_wday, p_tm->tm_yday, p_tm->tm_isdst );
delay(60000);
}

If I then install timelib: pio lib install 44, I can not build the same project although it’s a different library:

src\Time_ntp_test01.cpp: In function ‘void setup()’:
src\Time_ntp_test01.cpp:16:23: error: ‘time’ was not declared in this scope
while (!time(nullptr)) {
^
src\Time_ntp_test01.cpp: In function ‘void loop()’:
src\Time_ntp_test01.cpp:23:23: error: ‘time’ was not declared in this scope
time_t t = time(NULL);

^
src\Time_ntp_test01.cpp:24:33: error: ‘localtime’ was not declared in this scope
struct tm* p_tm = localtime(&t);
^
src\Time_ntp_test01.cpp:25:26: error: ‘ctime’ was not declared in this scope
Serial.println(ctime(&t));
^
src\Time_ntp_test01.cpp:27:63: error: invalid use of incomplete type 'struct tm’
p_tm->tm_year+1900, p_tm->tm_mon+1, p_tm->tm_mday,
^

I have installed this library in arduino IDE and it builds normally.

Hi @olon!
I suppose you are a Windows user? Windows system is not a case-sensitive so when your source file contains the line #include <time.h> PlatformIO decides to use library 44 because it has the similar file Time.h. As a workaround, you can just rename file Time.h to _Time.h in library directory c:\Users\your_user\.platformio\lib\Time_ID44 and your project should compile without errors.
We will consider the possibility of how to avoid this behaviour.
Sorry for the inconvenience.

Yes it builds now. Thank you !

Hi @valeros,
I am also a Windows user. Have been using PlatformIO for over a year now with no problem. However, I recently updated Atom, PlatformIO and all components to the latest version and now I have the same problem as the original poster. I went to see if your solution from a year and a half ago would work, but I don’t have any files or folders in my c:\Users\your_user.platformio\lib folder.
I guess, since then files have moved around, so I did a quick search on time.h, and these are the time.h files I found:
.platformio\packages\framework-arduinoespressif8266\tools\sdk\libc\xtensa-lx106-elf\include\time.h
.platformio\packages\framework-arduinoespressif8266\tools\sdk\libc\xtensa-lx106-elf\include\machine\time.h
.platformio\packages\framework-arduinoespressif8266\tools\sdk\libc\xtensa-lx106-elf\include\sys\time.h
.platformio\packages\framework-arduinoespressif8266\tools\sdk\lwip\include\lwip\app\time.h
.platformio\packages\toolchain-xtensa\xtensa-lx106-elf\include\time.h
.platformio\packages\toolchain-xtensa\xtensa-lx106-elf\include\machine\time.h
.platformio\packages\toolchain-xtensa\xtensa-lx106-elf\include\sys\time.h

Should I do something with one/all of these? What am I missing?

viktak

I’ve run into this problem even with V4.0 PlatformIO. The workaround works for Windows but is there a fix for this so it works out of the box ?

I suspect not… we don’t have a time machine, so we can’t go back and slap silly whoever thought calling a third party library the exact same name as basically one of the standard libraries was a bright idea, so that we don’t have this conflict in the future. It comes up time and time again on both Arduino IDE, PlatformIO and other compilers… it’s no longer funny. :-/