views:

85

answers:

5

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?

A: 

You can't. C Preprocessor definitions have absolutely nothing to do with environment variables. You would have to have each user having their own project files, and each user would need to add an additional define directive to the project file itself, which would be passed to cl.exe as a /D switch.

Billy ONeal
A: 

I just did a quick test and apparently VC++ 2005 doesn't automatically convert environment variables to preprocessor symbols (they are two completely different things).

So you would need to change some setting so that each user gets his symbol defined.

Bastien Léonard
+1  A: 

Edit: I have't tried my answer, because I'm currently working under Eclipse/Linux and thus can't test it, but I think it should work. (The corresponding Linux / Eclipse variant works fine).


You have to pass the environment variable in the compiler invocation (or build script / Makefile) as a /D %YOUR_NAME% switch.

To avoid problems when %YOUR_NAME% is not defined, you could add another String in front of it, e.g.

/D NAME_%YOUR_NAME%

Then you can use

#if defined(NAME_identitycrisisuk)

or whatever your username is.

IanH
Testing environment variables with !if in a makefile is common too.
Hans Passant
+1  A: 

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,
James Curran
This seems closest to the desired effect, I must have misremembered the setup, it seems fine if you just put $(YOUR_NAME) in the preprocessor defs? This means that I can also just add it to the debug build so there is no chance of a release version with behaviour unique to a computer, it was mostly used to tidy up extra debug output that no one else is interested in.
identitycrisisuk
Just adding $(Your_name) would be like saying `#define James_Curran`
James Curran
Yes, that was the desired effect, I don't think the initial question described it very well as I was misremembering it.
identitycrisisuk
+1  A: 

Rather scary concept, but if you really want to do so you could specify a preprocesor definition in Visual Studio project settings like this: YOUR_NAME=$(USERNAME) since windows machines have a %USERNAME% environment variable defined. If you are interested in using this from command line (or within a Makefile for that matter) you could specify /DYOUR_NAME=%USER_NAME% as an argument for cl.exe.

PS: Even though what you described is possible, you might want to reconsider this practice. If you would want certain functionality to be enabled only in certain scenarios, consider using some other switches that enable/disable certain features, e.g: registry values, configuration files and any other flag you would find reasonable. This would at least maintain the possibility for the same binaries to be tested by all of your colleagues. This in fact is a rather common practice, used sometimes in environments with continuous integration in place, so that you could integrate early and still not make others wait for you to finish or fix the feature. This conditional switch is of course removed as soon as the feature is even remotely seems to be ready to play well with the rest of your product.

Dmitry