Ad

Conditional Compilation For Working At Home

- 1 answer

I code C++ using MS Dev Studio and I work from home two days per week. I use CVS to keep my sources synchronized between the two computers but there are difference between the environments the machines are in.

Can anyone suggest a way I can conditionally modify constants in my code depending on whether I am compiling on my home box or not ?

What I am after is a way of defining a symbol, let's call it _ATHOME, automatically so I can do this:

#ifdef _ATHOME
#  define TEST_FILES  "E:\\Test"
#  define TEST_SERVER "192.168.0.1"
#else
#  define TEST_FILE   "Z:\\Project\\Blah\\Test"
#  define TEST_SERVER "212.45.68.43"
#endif

NB: This is for development and debugging purposes of course, I would never release software with hard coded constants like this.

Ad

Answer

On your home and work machines, set an environment variable LOCATION that is either "1" for home or "2" for work.

Then in the preprocessor options, add a preprocessor define /DLOCATION=$(LOCATION). This will evaluate to either the "home" or "work" string that you set in the environment variable.

Then in your code:

#if LOCATION==1
  // home
#else
  // work
#endif
Ad
source: stackoverflow.com
Ad