我可以在代码中设置 Visual Studio 2005 命令行开关(即通过 pragma/define)吗?

发布于 2024-12-09 11:07:57 字数 108 浏览 0 评论 0原文

我知道您可以提供命令行或在项目属性中添加开关,但是您可以从源文件中执行此操作吗?我需要为某些源文件设置某些开关,而无需进入项目属性并每次手动执行。那么也许您可以使用 C++ 源文件中的预处理器来完成它?

I know that you can provide a command line or add switches in your project properties but can you do that from source file? I need to set certain switches for certain source files without going into project properties and manually doing it everytime. So maybe you can do it with preprocessors from C++ source file?

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

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

发布评论

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

评论(3

山川志 2024-12-16 11:07:57

例如,有一些 #pragma 可以改变代码块的代码生成/优化,但我认为这是非常有限的。

There're some #pragma's that can, for example, alter code generation/optimization for a block of code, but I think that's pretty limited thing.

初见你 2024-12-16 11:07:57

第一:编译/链接不会执行您的 C++ 代码。因此,您无法编写一段 C++ 代码并根据某些 C++ 条件(如 if/else/...)做出决定(更改此项目的某些项目设置)。

正如您所知,#pragma#defines 是预处理器指令。尽管编译器理解#pragmas,但编程能力是有限的,并且通常是特定于编译器的。

它们的一个常见用途是使代码平台独立。

要回答您最初的问题“如何以编程方式(而不是手动)修改项目属性?”:您可以使用 #pragmas & 编写“代码”代码中的#defines(甚至是条件定义)来控制各种设置。通常,支持是特定于编译器供应商的,因此除非您小心,否则您的代码很可能无法(跨编译器)移植。

在我的 GCC 上,我总是发现 c++config.ha 是各种 cpp 指令的好例子。在我的 Windows PC 上,它安装在 X:\MinGW\lib\gcc\mingw32\4.5.2\include\c++\mingw32\bits\c++config.h

要查找支持的 cpp 指令,编译器的 RTM。


另一个选项显然是编写 shell/perl/... 脚本来修改项目设置/Makefiles。与让自己陷入 cpp 指令相比,我建议这样做。 :)

当然,此选项会在编译之前更改设置,而不是在编译期间更改设置。但如果这对您来说不是问题,请使用此指令而不是 cpp 指令。

First: Compilation/linking do not execute your C++ code. So you can't write a C++ piece'o code and make decision (to change some project settings of THIS project) based on some C++ conditions (like if/else/...).

#pragma and #defines are pre-processor directives as you already know. Though compiler understands #pragmas, programming capability is limited and usually compiler specific.

A common use of these is to make the code platform independent.

To answer your original question "how do I modify the project properties progrmatically (not manuall)?": You can write "code" using #pragmas & #defines (even conditional ones) in your code to control various settings. Usually support is compiler-vendor specific, so your code will most probably not be portable (across compilers) unless you're careful.

On my GCC, I always found c++config.h a good example of various cpp directive. On my windows PC it's installed in X:\MinGW\lib\gcc\mingw32\4.5.2\include\c++\mingw32\bits\c++config.h

To find out supported cpp directives, RTM of your compiler.


The other option obviously is to write shell/perl/... script to make modifications to the project settings/Makefiles. I recommend that compared to getting yourself tangled in cpp directives. :)

Of course this option would change the settings before compilation and not during. But if that's not a prob for you, go with this rather than cpp directives.

会傲 2024-12-16 11:07:57

正如已经提到的,#pragma 语句是非常特定于供应商的。对于 Visual Studio 2005,请在此处查看支持的 #pragma 语句:编译指令

我还想与 thekashyap 讨论编写脚本来修改每个文件的项目设置,原因如下:

  • 如果您必须添加或更改设置,则可以在*单个*位置进行修改。
  • 因为只有一个地方可以查找,所以未来的维护人员知道在哪里可以找到给定文件的特殊设置。他们不会对隐藏在项目设置对话框深处的手动设置感到惊讶。

As has been mentioned, #pragma statements are very vendor specific. For Visual Studio 2005, take a look here for the supported #pragma statements: Pragma Directives

I would also with thekashyap regarding writing a script to modify the project settings for each file for the following reasons:

  • In the event that you have to add or change a setting, there is a *single* place to modify.
  • Because there is only one place to look, future maintainers know where to find special settings for a given file. They won't be surprised by something set manually that is hidden deep within the project settings dialogs.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文