如何在编译时判断环境变量是否存在?

发布于 2024-09-13 17:25:44 字数 329 浏览 4 评论 0原文

我不太知道这是否是一件好事,但我曾经工作过,每个人的计算机上都有一个像 YOUR_NAME 这样的环境变量。然后,如果您有一些只对您自己感兴趣的调试代码,您可以将其包装在 #if Defined( YOUR_NAME ) 中,并且它甚至不会为其他人编译,除非他们将其更改为#if 已定义( YOUR_NAME ) ||已定义(THEIR_NAME)

我刚刚尝试自己执行此操作,但似乎不起作用,重新启动了 Visual Studio,然后重新启动了计算机,但似乎仍然没有启动。这个黑客攻击是否比我理解的更多,或者它是否需要在 Visual Studio 中选择特定版本/选项?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱人如己 2024-09-20 17:25:44

在 IanH 设置的基础上,在 Visual Studio 中

  • 右键单击“项目资源管理器”面板中的项目名称。
  • 选择“属性页”
  • “打开配置属性”、“C/C++”、“预处理器”(即 VS2008 位置,但在 vs2005 中应该类似)
  • 对于“预处理器定义”,应该有 WIN32;_DEBUG 和其他内容。最后,添加 ;YOUR_NAME="$(YOUR_NAME)"
  • 请注意,如果 evar YOUR_NAME 中有空格,它会变得非常沮丧,

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,
叹梦 2024-09-20 17:25:44

相当可怕的概念,但如果你真的想这样做,你可以在 Visual Studio 项目设置中指定一个预处理器定义,如下所示: YOUR_NAME=$(USERNAME) 因为 Windows 计算机有一个 %USERNAME% 已定义环境变量。如果您有兴趣从命令行(或在 Makefile 中)使用它,您可以指定 /DYOUR_NAME=%USER_NAME% 作为 cl.exe 的参数。

PS:尽管您所描述的情况是可能的,但您可能需要重新考虑这种做法。如果您希望仅在某些情况下启用某些功能,请考虑使用一些其他开关来启用/禁用某些功能,例如:注册表值、配置文件和您认为合理的任何其他标志。这至少可以保持所有同事测试相同二进制文件的可能性。事实上,这是一种相当常见的做法,有时在持续集成到位的环境中使用,因此您可以尽早集成,并且仍然不会让其他人等待您完成或修复该功能。当然,一旦该功能似乎已经准备好与产品的其余部分很好地配合使用,这个条件开关就会被删除。

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.

错々过的事 2024-09-20 17:25:44

编辑:我还没有尝试过我的答案,因为我目前正在 Eclipse/Linux 下工作,因此无法测试它,但我认为它应该可以工作。 (相应的 Linux / Eclipse 变体工作正常)。


您必须在编译器调用(或构建脚本/Makefile)中将环境变量作为 /D %YOUR_NAME% 开关传递。

为了避免在未定义 %YOUR_NAME% 时出现问题,您可以在其前面添加另一个字符串,例如

/D NAME_%YOUR_NAME%

然后您可以使用

#if defined(NAME_identitycrisisuk)

或 无论您的用户名是什么。

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.

靖瑶 2024-09-20 17:25:44

你不能。 C 预处理器定义与环境变量完全无关。您必须让每个用户都有自己的项目文件,并且每个用户都需要向项目文件本身添加一个额外的定义指令,该指令将作为 /D 开关传递给 cl.exe 。

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.

寂寞笑我太脆弱 2024-09-20 17:25:44

我刚刚做了一个快速测试,显然 VC++ 2005 不会自动将环境变量转换为预处理器符号(它们是两个完全不同的东西)。

因此,您需要更改一些设置,以便每个用户都能定义自己的符号。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文