putenv warning with C++

一个人想着一个人 提交于 2020-05-15 09:04:05

问题


I am trying to use putenv stdlib function in a program that I am compiling with g++ including the flags and warnings -std=c++11 and -Wall -Wextra.

The program can be as easy as the following:

#include<stdlib.h>
#include<iostream>
int main(int argc, char *argv[])
{
    putenv("LD_LIBRARY_PATH=../Desktop/lib");
    std::cout<<"hello\n";
    return 0;

}

but I am getting this error warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings].

As far as I understood in C++ I should declare: char const *str = but then putenv is complaining.

I know I could cast but is there a proper way to use putenv function in C++ or being it a C function it should be completely avoided?


回答1:


While std::getenv is part of the C++ standard (and the C standard as well, but in the future pick one language, the one you really program in which in your case is C++), the putenv function isn't.

As you can see from the linked POSIX reference for putenv, it's argument is of type char *.

This is very important, and one thing that differs between C and C++: In C a literal string can be passed to functions expecting char *. In C++ all literal strings are constant and can only be passed to functions expecting const char *.

To solve your problem, you need to use a non-constant array that you initialize and then pass:

char env[] = "LD_LIBRARY_PATH=../Desktop/lib";
putenv(env);

Important note: The array must be valid during the full life-time of your program. That means even after the main function returns.

A better solution (and mentioned in a comment) is the setenv function, which both takes const char * for its argument (and can therefore be used with literal strings) and also copies the arguments which means there's no problem about scope and life-time.


Regarding string literals. In both C and C++ they are really arrays of characters. The difference is that in C++ the arrays are constant.




回答2:


The problem is already pointed out by other answers. In C++ a string literal is of type const char[] while in C it is char[], but modifying it will lead to undefined behavior.

According to the man page:

The putenv() function adds or changes the value of environment variables. The argument string is of the form name=value. If name does not already exist in the environment, then string is added to the environment. If name does exist, then the value of name in the environment is changed to value. The string pointed to by string becomes part of the environment, so altering the string changes the environment.

...

Thus, it is an error is to call putenv() with an automatic variable as the argument, then return from the calling function while string is still part of the environment.

You should be careful when you call putenv with a variable of automatic storage (for example if you call it in a function other than main) since the pointer becomes part of the environment.




回答3:


An alternative is to follow the putenv manpage's advice that you should instead use the more modern and safe setenv, which does not have this problem, nor any of the complexities that arise from it.




回答4:


putenv get a (non const) char * and you give a const char *, the compiler is not happy and this normal

just do

char s[] = "LD_LIBRARY_PATH =../Desktop/lib";

putenv(s);


来源:https://stackoverflow.com/questions/54279450/putenv-warning-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!