mercredi 17 août 2016

Proper setting a local environment variable in C++

In my code I'm using the following:

putenv("TZ=UTC");
tzset();

to set the timezone.

Declaration of putenv() (this answer recommended it to set the environment variable):

int putenv(char *string);

The buildsystem I'm using sets compiler flags -Wall -Wextra -Werror -std=c++0x and due to that I'm getting the error:

timeGateway.cpp:80:18: error: ISO C++ forbids converting a string constant to 'char*' [-Werror=write-strings]
   putenv("TZ=UTC");
                  ^

I know that this error can be suppressed by using:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
  putenv("TZ=UTC");
#pragma GCC diagnostic pop

But this is very ugly.

My question: what is a proper way to set an environment variable in C++?

Aucun commentaire:

Enregistrer un commentaire