简体   繁体   English

在家工作的条件汇编

[英]Conditional compilation for working at home

I code C++ using MS Dev Studio and I work from home two days per week. 我使用MS Dev Studio编写C ++,我每周工作两天。 I use CVS to keep my sources synchronized between the two computers but there are difference between the environments the machines are in. 我使用CVS使我的源在两台计算机之间保持同步,但机器所处的环境之间存在差异。

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: 我所追求的是一种定义符号的方法,让我们自动称它为_ATHOME,所以我可以这样做:

#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. 注意:这当然是出于开发和调试的目的,我绝不会发布像这样的硬编码常量的软件。

On your home and work machines, set an environment variable LOCATION that is either "1" for home or "2" for work. 在您的家庭和工作机器上,设置一个环境变量LOCATION ,对于家庭来说是“1”,对于工作来说是“2”。

Then in the preprocessor options, add a preprocessor define /DLOCATION=$(LOCATION). 然后在预处理器选项中添加预处理器define / DLOCATION = $(LOCATION)。 This will evaluate to either the "home" or "work" string that you set in the environment variable. 这将评估您在环境变量中设置的“home”或“work”字符串。

Then in your code: 然后在你的代码中:

#if LOCATION==1
  // home
#else
  // work
#endif

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. 如果工作和家庭之间的唯一区别是测试文件所在的位置...那么(恕我直言)你不应该用一堆静态路径和IP污染你的构建文件。

For the example you showed, I would simply map drives on both work and home. 对于您展示的示例,我只是在工作和家庭上映射驱动器。 Ie 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. 即在工作地图上的一个驱动器T:指向\\\\ 212.45.68.43 \\ Project \\ Blah \\ Test,在主地图上一个驱动器T:指向\\\\ 192.168.0.1 \\ Test。

Then your build process uses the path "T:\\" to refer to where tests reside. 然后,您的构建过程使用路径“T:\\”来引用测试所在的位置。

Of course, if you need to change something more drastic, setting environment variables is probably the best way to go. 当然,如果你需要更改一些更激烈的东西,设置环境变量可能是最好的方法。

You can set preproccesor variables in the properties->c++->preprocessor 您可以在properties-> c ++ - >预处理器中设置preproccesor变量
in visual studio settings you can use $(enviromentvariable) 在visual studio设置中你可以使用$(enviromentvariable)

我通常使用配置文件,然后只需创建一个符号链接到适当的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM