views:

239

answers:

4

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.

+2  A: 

You can set preproccesor variables in the properties->c++->preprocessor
in visual studio settings you can use $(enviromentvariable)

Martin Beckett
Sorry mgb, even though your answer is essentially the same, 1800 INFORMATION's, answer is worded better and includes an example so I'm going to accept that one.
Adam Pierce
+1  A: 

I generally use config files, then just create a symlink to the appropriate configuration.

Don Neufeld
+6  A: 

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
1800 INFORMATION
Brilliant, that's just what I was looking for. I can probably use a similar technique on Linux too.
Adam Pierce
+2  A: 

If the only difference between work and home is where the test files are located... then (IMHO) you shouldn't pollute your build files with a bunch of static paths & IPs.

For the example you showed, I would simply map drives on both work and home. I.e. at work map a drive T: that points to \\212.45.68.43\Project\Blah\Test, at home map a drive T: that points to \\192.168.0.1\Test.

Then your build process uses the path "T:\" to refer to where tests reside.

Of course, if you need to change something more drastic, setting environment variables is probably the best way to go.

ceretullis