2023-09-20 09:07 AM
Is there a way to read an environment variable from the PC (I'm running on a Windows box) and pass that value somehow (maybe via a temporarily created define for the command line) such that a file being compiled can use the value of that environment variable? For example, can the name of the person who's doing the build be used to initialize a string value that can then be embedded into the app?
Thanks!
Solved! Go to Solution.
2023-09-20 10:55 AM
Yes, but converting it to a string is awkward.
Add "USERNAME=$(USERNAME)" switch to your preprocessor defines.
Then, in your program:
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
...
printf("username=%s\n", TOSTRING(USERNAME));
Might be a way to add the quotes in the define itself but I couldn't figure it out. The shell is fighting itself with expanding things.
2023-09-20 10:55 AM
Yes, but converting it to a string is awkward.
Add "USERNAME=$(USERNAME)" switch to your preprocessor defines.
Then, in your program:
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
...
printf("username=%s\n", TOSTRING(USERNAME));
Might be a way to add the quotes in the define itself but I couldn't figure it out. The shell is fighting itself with expanding things.
2023-09-20 11:20 AM
Accepted as solution, but after further investigation https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification.html there is no way to convert a macro argument into a character constant, and gcc generates an error any way I try to convert USERNAME into something I can use as a const char*.
2023-09-20 11:39 AM
I tested it. It works for me. Win10 STM32CubeIDE.
What error are you getting?