1

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
Lasersköld
  • 2,028
  • 14
  • 20
  • 3
    *"I know how to do in linux, and windows with msvc and clang"* You sure none of those solutions work with mingw? – HolyBlackCat Mar 20 '21 at 14:32
  • Don't use windows, but maybe you could use `system()` to set environment variables through the cmd? I just checked and apparently [this](https://ss64.com/nt/setx.html) is the command to set environment variables. Perhaps you could run this through `system("setx ENVV WHATEVER")`? Not sure about getting though. – mediocrevegetable1 Mar 20 '21 at 14:33
  • @HolyBlackCat I thought that by asking the question it would be obvious that I have tried them without success. But I did an update to the question if what you really wanted was to se what I have tried. – Lasersköld Mar 20 '21 at 14:44
  • it seems to suggest `_putenv_r` when you try `_putenv_s`. Do you know what that does? – mediocrevegetable1 Mar 20 '21 at 14:46
  • 1
    *"would be obvious that I have tried them"* But we don't know what exactly you did or didn't try. What about `putenv`? Also [this](https://www.gnu.org/software/gettext/FAQ.html#windows_setenv) looks promising – HolyBlackCat Mar 20 '21 at 14:47
  • 1
    Oh, found [this thread on `_putenv_s` for mingw](https://github.com/mxe/mxe/issues/1448). May be helpful. – mediocrevegetable1 Mar 20 '21 at 14:51
  • @S.M. It is not defined, but I found this that might be used: https://stackoverflow.com/questions/21826649/boost-test-on-windows-with-mingw-compiler-error-putenv-not-declared If i add a own definition it compiles a tleast. – Lasersköld Mar 20 '21 at 14:57
  • 1
    Not sure about mingw, but you can use `_putenv_s` with mingw-w64. – ssbssa Mar 20 '21 at 15:13

1 Answers1

0

With inspirations from the comments i found this question on putenv

and managed to whip together this prototype application:


#include <cstdlib> // For system(...)

#ifdef __MINGW32__
extern int putenv(char *); // Not defined by mingw
#endif

int main() {
    putenv(const_cast<char *>("x=10"));
    system("set"); // Output variables to se if it works

    return 0;
}

Resulting output:

....
x=10

Thanks for the help!

Lasersköld
  • 2,028
  • 14
  • 20