PlatformIO Community

Identifier is undefined - setenv tzset

Hi,

Is anybody familiar with below error on compiling code for esp32 based on arduino framework in platformio ?

identifier “setenv” is undefined
identifier “tzset” is undefined

I am working on an ubuntu machine. It appears setenv is part of stdlib.h and tzset is part of time.h. But i think the arduino framework libraries are not having these. These appear to be part of regular c/c++ libraries which am not sure if my program compilation is seeing them ?

  1. For arduino framework to support it, does some special libraries needs to be installed ? How ?
  2. Can i use the external gcc c/c++ library paths into this esp32 project ? How ?

Thanks,
Rajesh

Code like

#include <time.h>
#include <cstdlib>

void setup() {
    setenv( "TZ", "PST8PDT", 1 );
    tzset(); 
}

void loop() {}

does compile and is advertised to work by the underlying ESP-IDF layer.

There is just an Intellisense error, which comes from the fact that there are multiple time.h files in the include path of the compiler and that a macro is set which comments out the definition of those functions.

Same for setenv()

And __STRICT_ANSI__ is defined by the compiler in the Intellisense, but obviously the /tools/sdk/lib/libc_nano.a contains the definitions so it was compiled without the strict ANSI.

Not sure how to put the compiler during Intellisense into the mode so that __STRICT_ANSI__ is not defined and these extended functions are compiled in. If you just want to get rid of the Intellisense errors you can copy-paste the declaration on top of the sketch.

#include <Arduino.h>

_VOID      _EXFUN(tzset,	(_VOID));
int	_EXFUN(setenv,(const char *__string, const char *__value, int __overwrite));
2 Likes

thanks a lot thats much cleaner.

i figured if i put #undef STRICT_ANSI , its working, but definitely your solution is better.

Thanks,
Rajesh

2 Likes

Hi

I used exactly this solution in my Arduino / ESP8266 project about a year ago thanks. However, now when I come to open the source code again, ironically, I see Intellisense errors.

Errors

Here are the respective errors -

  • identifier “_VOID” is undefined
  • identifier “tzset” is undefined
  • identifier “_VOID” is undefined
  • identifier “setenv” is undefined
  • expected a ‘)’
  • expected an expression
  • type name is not allowed
  • expected a ‘)’

Any ideas please ?

Thanks

Does the project still build?

Thanks for the prompt reply.

No, it doesn’t build, failing with these errors -

25 | #include <Adafruit_I2CDevice.h>
| ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
*** [.pio\build\nodemcuv2\lib3b7\RTClib\RTC_PCF8523.cpp.o] Error 1
src\main.cpp:18:1: error: ‘_VOID’ does not name a type
18 | _VOID _EXFUN(tzset, (_VOID));
| ^~~~~
src\main.cpp:19:20: error: expected primary-expression before ‘const’
19 | int _EXFUN(setenv,(const char *__string, const char *__value, int __overwrite));
| ^~~~~
src\main.cpp:19:20: error: expected ‘)’ before ‘const’
19 | int _EXFUN(setenv,(const char *__string, const char *__value, int __overwrite));
| ~^~~~~
| )

In file included from C:\Users\netdu.platformio\lib\RTClib\src\RTC_DS1307.cpp:1:
C:\Users\netdu.platformio\lib\RTClib\src\RTClib.h:25:10: fatal error: Adafruit_I2CDevice.h: No such file or directory

I found this article and managed to get the code to compile further after I used the suggested option to add the Adafruit Bus IO library into the project.

I still get the below errors though -

src\main.cpp:18:1: error: ‘_VOID’ does not name a type
18 | _VOID _EXFUN(tzset, (_VOID));
| ^~~~~
src\main.cpp:19:20: error: expected primary-expression before ‘const’
19 | int _EXFUN(setenv,(const char *__string, const char *__value, int __overwrite));
| ^~~~~
src\main.cpp:19:20: error: expected ‘)’ before ‘const’

As it happens, you previously helped someone else with the same Adafruit issue

Thanks again.

I’ll add that the code compiles ok if I comment out those two lines.

I do get the below warning though, which I guess is the Intellisense issue ?

identifier “setenv” is undefined

Ehm this thread is originally about ESP32, not sure if that 1:1 applies to your ESP8266 project… Because in the ESP8266 toolchain it has

#if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112
int	setenv (const char *__string, const char *__value, int __overwrite);
#endif

and

#if __POSIX_VISIBLE
void      tzset 	(void);
#endif

which is different from what ESP32 has.

However adding the declaration like

#include <Arduino.h>

extern "C" int setenv (const char *__string, const char *__value, int __overwrite);
extern "C" void tzset();

void setup() {
    setenv("TZ", "GMT", 1/*overwrite*/);
    tzset();
}

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

should work.

A different way to solve the problem is to add

build_flags = -D_BSD_SOURCE

to the platformio.ini, then no extra declarations are necessary.

1 Like

Thanks Max. That fixed it. Seriously, a big thanks from me for helping out with this.