I am writing a program that needs to set environment variables for the current process from mingw (to be available for subprocesses when using system(...)
-call).
I know how to do in linux, and windows with msvc and clang. However, I cannot find any goood examples on how to do it with mingw-g++.
How do I implement functions that has this kind of behaviour?
// Example usage:
void setVar(std::string name, std::string value) {
// How to do this
}
std::string getVar(std::string name) {
// ... and this
}
If you want to answer in c, feel free to omit std::string :)
Edit:
When using setenv
(the linux way) i get:
src/env.cpp: In function 'void appendEnv(std::string, std::string)':
src/env.cpp:46:5: error: 'setenv' was not declared in this scope; did you mean 'getenv'?
46 | setenv(name.c_str(), value.c_str(), 1);
| ^~~~~~
| getenv
When using _putenv_s (the way i have used for msvc and clang on windows) I get.
src/env.cpp: In function 'int setenv(const char*, const char*, int)':
src/env.cpp:16:12: error: '_putenv_s' was not declared in this scope; did you mean '_putenv_r'?
16 | return _putenv_s(name, value);
| ^~~~~~~~~
| _putenv_r