我可以在代码中设置 Visual Studio 2005 命令行开关(即通过 pragma/define)吗?
我知道您可以提供命令行或在项目属性中添加开关,但是您可以从源文件中执行此操作吗?我需要为某些源文件设置某些开关,而无需进入项目属性并每次手动执行。那么也许您可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
例如,有一些 #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.
第一:编译/链接不会执行您的 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.
正如已经提到的,
#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 DirectivesI would also with thekashyap regarding writing a script to modify the project settings for each file for the following reasons: