有没有办法在运行时设置#define 的值?
我想知道是否有办法在运行时设置#define 的值。
我假设下面的代码中有针对 Oracle 特定和 Sql Server 特定的查询。
#define oracle
// ...
#if oracle
// some code
#else
// some different code.
#endif
I wonder if there is a way to set the value of #define in run time.
I assume that there is a query for Oracle specific and Sql Server specific at the code below.
#define oracle
// ...
#if oracle
// some code
#else
// some different code.
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
绝对不是,#defines 是在编译器看到它之前就由预处理器编译出来的 - 因此标记“oracle”甚至不在您的代码中,只是“1”或“0”。 将 #define 更改为全局变量或(更好)返回正确值的函数。
Absolutely not, #defines are compiled out by the preprocessor before the compiler even sees it - so the token 'oracle' isn't even in your code, just '1' or '0'. Change the #define to a global variable or (better) a function that returns the correct value.
#if
是编译时的。 您可以在构建过程中指定这一点(通过切换到 msbuild/csc),但实际上不是在运行时指定。 排除的代码不存在。 最好建议您(其中之一):#if
is compile-time. You could specify this in your build process (via switches to msbuild/csc), but not really at runtime. The excluded code doesn't exist. You might be better advised to (1 of):不,预处理器在编译之前运行,并且可以在那时更改代码,这就是它的目的,如果您想在运行时根据某些内容切换行为,请使用变量和正常的条件逻辑。
No, the preprocessor runs before compile and can alter code at that time, that is its purpose, if you want to switch behavior based on something at runtime use a variable and normal conditional logic.
完整说明:这些针对 OpenCL 的优化幻灯片意味着在第 11 页,这是可能的,但请注意,作者似乎将“运行时”解释为船体程序的运行时。 从这个意义上说,内核(每个 GPU 线程的小程序)是“在运行时编译的”。
To be complete: These optimization slides for OpenCL imply at page 11 that it is possible, but note that the author seems to interpret "runtime" as the hull program's runtime. In this sense, the kernels (small programs for each GPU thread) are "compiled at runtime".