C++: Setenv(). Undefined identifier in Visual Studio
Asked Answered
M

4

21

Look my code seems to be correct, according to all the documentation I can find online. My IDE is MS Visual Studio Xpress 4 Windows Desktop 2012, and it's compiler is throwing up the error:

Error 1 error C3861: 'setenv': identifier not found e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 18 1 Project1.

Help me!!!

#include <windows.h>
#include <sstream>
#include <ostream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>

using namespace std;

int howManyInClass = 0;
int main(){

long checklength = sizeof(getenv("classSize"))/sizeof(*getenv("classSize"));
if (checklength==0){
    cout<<"Please enter the ammount of students in your class";
    cin>> howManyInClass;
    cin.ignore();
    setenv("classSize", howManyInClass, 1);}

};
Marela answered 23/6, 2013 at 5:43 Comment(1)
Wait, I shouldnt need 2 as I have already used std as a namespaceMarela
C
16

You can either use _putenv() which takes a string parameter as the string classSize=7;

ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());

...or (preferably) the security enhanced _putenv_s() that takes the key and the value as separate (const char*) parameters;

ostringstream classSize;
classSize << howManyInClass;
_putenv_s("classSize", classSize.str().c_str());
Chinn answered 23/6, 2013 at 6:17 Comment(2)
Could you help me w the error: 'Error 1 error C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 12 1 SchoolManagementSystme' ThanksMarela
_putenv() _putenv_s links appear dead.Oren
C
35

Microsoft's runtime library doesn't support the standard setenv() function. You could use their replacement _putenv() or, for portable code, I prefer to use a simple wrapper.

Here's my wrapper with the standard interface:

int setenv(const char *name, const char *value, int overwrite)
{
    int errcode = 0;
    if(!overwrite) {
        size_t envsize = 0;
        errcode = getenv_s(&envsize, NULL, 0, name);
        if(errcode || envsize) return errcode;
    }
    return _putenv_s(name, value);
}
Celibate answered 12/5, 2014 at 18:25 Comment(1)
I tried also SetEnvironmentVariable(name, value) from processenv.h on Windows, but I made sure that it is buggy. The LoadLibrary() API does not see the PATH modified by SetEnvironmentVariable() for some reason. _putenv_s() worked fine for my case.Grasso
C
16

You can either use _putenv() which takes a string parameter as the string classSize=7;

ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());

...or (preferably) the security enhanced _putenv_s() that takes the key and the value as separate (const char*) parameters;

ostringstream classSize;
classSize << howManyInClass;
_putenv_s("classSize", classSize.str().c_str());
Chinn answered 23/6, 2013 at 6:17 Comment(2)
Could you help me w the error: 'Error 1 error C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 12 1 SchoolManagementSystme' ThanksMarela
_putenv() _putenv_s links appear dead.Oren
C
1

Try _putenv instead of setenv.

msdn _putenv

Clincher answered 23/6, 2013 at 6:4 Comment(1)
That's better solution for secure one, but keep mind that's support only after VS2005.Clincher
B
0

the reason you encountered the linkage error is that, if you take a look at the content of the library of stdlib.h, you will find that, setenv() is not declared there. At the first glance, it is a C standard API, but looks like Windows do not follow all of the standard. Or, you might be able to configure your VS to use CRT instead of Windows runtime, in that case, I think setenv will be identified.

Baking answered 2/12, 2014 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.