Search code examples
c++environment-variablesvisual-studio-2005c-preprocessor

How can you find whether an environment variable exists at compile time?


I don't particularly know if this is a good thing or not but I used to work somewhere where everyone had an environment variable like YOUR_NAME on their computer. Then if you had a bit of debug code that was only of interest to yourself you could wrap it in #if defined( YOUR_NAME ) and it wouldn't even be compiled for someone else unless they changed it to #if defined( YOUR_NAME ) || defined( THEIR_NAME ).

I've just tried to do this myself and it doesn't seem to work, restarted Visual Studio and then the computer but it still doesn't seem to be picked up. Was there more to this hack than I understood or does it require a specific version/option selected in Visual Studio?


Solution

  • Building on what IanH set, from withing Visual Studio,

    • right click the project name in the Project Explorer panel.
    • Choose Property Page
    • Open Configuration Properties, C/C++, Preprocessor (that is the VS2008 location but it should be similar in vs2005)
    • For the Preprocessor Definitions, there should be WIN32;_DEBUG and prehaps others. At the end, add ;YOUR_NAME="$(YOUR_NAME)"
    • Note that it get very upset is there is a space in the evar YOUR_NAME,