Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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++?

like image 398
lewiatan Avatar asked Aug 17 '16 09:08

lewiatan


2 Answers

The string literal is const, its type is const char[] (for "TZ=UTC" it'll be const char[7], including the trailing null character '\0'), which can't be assigned directly to a (non-const) char* from C++11.

You could construct a new char array for it.

char str[] = "TZ=UTC"; // initialize a char array, which will contain a copy of the string "TZ=UTC"
putenv(str);
like image 64
songyuanyao Avatar answered Sep 30 '22 11:09

songyuanyao


putenv normally allows the string to be changed after the call to putenv and that actually automatically changes the environment. That is the reason why the prototype declares a char * instead of a const char *, but the system will not change the passed string.

So this is one of the rare correct use cases for a const cast:

putenv(const_cast<char *>("TZ=UTC"));

Alternatively, you could use setenv that takes const char * parameters:

setenv("TZ", "UTC", 1);
like image 34
Serge Ballesta Avatar answered Sep 30 '22 09:09

Serge Ballesta