Conditional Compilation

Programming_in_Lua,_4th_ed._(2017)_.pdf Programming in lua. forth edition. lua程序设计第四版 立即下载
Conditional Compilation
Although debugging techniques can help you iron out problems in your code,
you may want to simply add chunks of code at compile time (if you're
currently in the debugging phase of your project) or remove the same chunks
when you release your code. Conditional compilation makes it possible to
add and remove chunks from the executable.

Conditional compilation relies on compiler directives in your code that
indicate to the compiler, as it's compiling your code, how it should use
the code. Compiler directives are #If statements that direct the compiler
as to which code it should leave in and which code it should leave out. For
example, you might wish to put debugging code in your application, but when
you create a final compile, you don't want the debugging code to be there.
Or maybe you wish to take out the code for certain features if you are
creating a demo version of your product.

To use conditional compilation, you will first need to declare a compiler
constant. You declare then using the #Const statement. Each constant is
given a unique name and assigned a value. Once these constants are
declared, you can use them within an #If… #End If block. As you'll see,
there are several ways to define conditional compilation constants.

Declaring a File-Level Compiler Constant
You can declare a compiler constant anywhere within a file in your program.
That compiler constant is then available anywhere within that file. It does
not matter whether you place the constant declaration at the top of the
file, within a defined class, or within a procedure, it will still be
available throughout the whole file. For example, the sample page
DebugForm.aspx defines the following constant at the top of its code-behind
file:

#Const DEMO = True

Using Conditional Compilation Constants
To create a compiler directive, use the #If… #End If construct. You might,
for example, want to include one block of code for demo versions of your
application and different code for retail versions, like this:

Private Sub ConditionalSample()
#If DEMO Then
  lblMessage.Text = "Demo version"
#Else
  lblMessage.Text = "Retail version"
#End If
End Sub

NOTE

You may be wondering why you'd use conditional compilation rather than
simple conditional statements in your code. One good reason is that you may
have large blocks of code that are different for two versions of your
application. There's no reason to load both sets of code when they're
mutually exclusive. Instead, you can make the choice as to which block you
want to include at compile time (not at runtime) using conditional
compilation.



Unlike a normal If statement, the #If statement actually removes the unused
code as it compiles. This leads to a reduced application size in memory and
prevents unnecessary or unwanted code being deployed as part of the
application. For the preceding example, the compiler will actually only see
the following code (assuming that the DEMO constant is set to True):

Private Sub ConditionalSample()
  lblMessage.Text = "Demo version"
End Sub

TIP

Visual Studio .NET provides two built-in compile-time constants: DEBUG and
TRACE. By default, they're both defined for you (although you can modify
this behavior using the Project Properties dialog box). The DEBUG constant
is defined to be True when you are compiling a Debug build of your program.
(You can choose what type of build you are making by selecting Build,
Configuration Manager from the menu bar and then choosing either the Debug
or Release version. Once you set your application to build as Release, the
Debug constant is set to False.) The TRACE constant allows you to control
runtime tracing of your application.



Declaring a Global Compiler Constant
To declare a compiler constant that can be used throughout your whole
application, select the project within the Solution Explorer window,
right-click, and select Properties from the context menu. On the Property
Pages dialog box, select Configuration Properties, Build. Figure 9.25 shows
the Property Pages dialog box.

Figure 9.25. Use this property page to set compile-time constants.


In the Custom Constants text box, enter the constant name, an equal sign,
and the value to assign. (Compile-time constants follow the same naming
rules as any other Visual Basic .NET variables.)
iOS(Swift) 条件编译, Active Compilation Conditions和Preprocessor Macros的区别 【已解决】Xcode中Active Compilation Conditions和Preprocessor Macros的区别 条件编译作用: 在实际开发中我们常常需要区分不同的环境,此处以最简单的开发与生产环境为例,每次打包通过修改代码区分不同的环境过于繁琐,并且如果需要修改的地方过多,忘改了某一处的话就会造成环境不统一,不仅给开发人员增加负担,对测试同事也是麻烦的一件事。因此,通过预处理宏... 阅读详情

相关推荐

编译的四个阶段

编译器编译时的四个阶段详细介绍

blue的博客 4225

#if #elif #endif 的使用--------条件编译(conditional compilation

使用 #if 以及 #else、#elif、#endif、#define 和 #undef 指令,可以包括或排除基于由一个或多个符号组成的条件的代码 一般条件编译用于你在调试的时候,需要输出结果来确定程序是否正确,但在发布的时候不需要输出这些结果。或者程序在某一版本发布了一个试用的功能,但在下一版本中要暂时去掉这个功能,就可以这个条件来控制。 #define DEBUG

圣杰 1232

C#中的预处理指令详解

这篇文章主要介绍了C#中的预处理指令详解,本文讲解了#define 和 #undef、#if、#elif、#else和#endif、#warning和#error、#region和#endregion、#line、#pragma等预处理指令,需要的朋友可以参考下 目录 1. #define 和 #undef2. #if、#elif、#else 和#endif3. #warnin...

weixin_30448603的博客 230

PL/SQL Conditional Compilation

该特性与C语言的类似。从Oracle 9.2.0.6开始被引入,9.2.0.6默认关闭该特性,可以通过一个参数打开。10gR1默认是打开,可以关闭。10gR2或以上默认打开,不可以关闭。常用于以下场景: 代码使用Oracle某新特性实现某个需求;为了兼容老版本Oracle,需要用另一个方法实现。在编译时,可以针对Oracle版本,选择性的编译。在开发时,打开Tracing, 生产服务器上关闭

chncaesar的专栏 1121

9.5.4 Conditional compilation directives

9.5.4 Conditional compilation directivesThe conditional compilation directives are used to conditionally include or exclude portions of a source file.pp-conditional::pp-if-section pp-elif-sectionsopt

★★创造未来★★ 905

Java 条件编译 Conditional Compilation

根据Java编译器的优化的机制,Java也能够提供条件编译。对于条件设为false的语句,编译器将不对条件覆盖的代码段生成字节码。 不仅可以用简单的boolean常量值来做条件编译,还可以用字符串常量及任何其他类型的常量. 例如: 1. 简单的boolean常量。 final boolean isDebug = true; if(isDebug) { // debug模式状态 ...

GarfieldEr007的专栏 893

JScript: Conditional Compilation

<br /><br />Conditional compilation allows the use of new JScript language features without sacrificing compatibility with older versions that do not support the features.<br />Conditional compilation is activated by using the @cc_on  statement, or using

FuDesign2008的专栏 795

Conditional Compilation Variables

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;script&gt;/*@cc_on @*//*@if (@_jscript_version &gt;= 4){    var s = ""   s += &qu

weixin_33835690的博客 84

17-conditional_compilation

条件编译 条件编译的行为类似于C语言中的if...else 条件编译是预编译指示命令,用于控制是否编译某段代码 /* #define C 1 */ int main() {     #if (C == 1)         printf("This is 1st printf ...\n");     #else         printf("This is 2nd prin

halazi100 576

9.5.1 Conditional compilation symbols

9.5.1 Conditional compilation symbolsThe conditional compilation functionality provided by the #if, #elif, #else, and #endif directives iscontrolled through pre-processing expressions (§9.5.2) and con

★★创造未来★★ 1154

[PLSQL] Conditional Compilation

在维护数据库安装脚本的时候发现一个很恶心的问题,就是为了考虑用户有没有安装某一个产品模块,需要安装不同版本的package (trigger),或者是有些package压根在某个版本中不用安装。这样老是维护两套脚本,但是两套脚本的差别又不是很大,维护的成本有点高而且相当的boring. 想到是否可以利用plsql 的conditional compilation的特性来将不同版本的packag...

weixin_30446613的博客 134

Conditional Compilation In Java @ JDJ

<!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--><script type="text/javascript"

zgqtxwd的专栏 450

java 条件编译(Conditional Compilation

条件编译是代码编译一大神器。 在C/C++编译中可有#ifdef...#endif这样的预编译宏。那么在java编译中,有什么呢? Java编译器优化机制中有这么一条:对于条件表达式中永远为false的语句,编译器将不对条件覆盖的代码段生成字节码。 例如: final boolean isDebug = false;   if(isDebug){       printf("I'm in debu...

pxxian的博客 741

IE 条件编译 conditional compilation

        IE 的条件编译是只有 IE 浏览器才支持的,其他的浏览器不支持,所以在需要特殊处理 IE 浏览器的时候可以使用条件编译。        为了使其他浏览器不会出问题,要把条件编译编译开关和特殊语句放到 /**/ 里面。        /*@cc_on @*/   // 打开 ie 条件编译,只有 ie 可以识别         利用条件编译区分 IE 浏览器和非 I

新博客地址: zhangl.in 830

Using conditional compilation[From adobe]

from adobe livedoc:http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html#246171 To include or exclude blocks of code for certain builds, you can use conditional compilation. The m...

weixin_30709635的博客 58

ASP.NET MVC View使用Conditional compilation symbols

由于View(.cshtml)的运行时编译关系,在项目级别中定义的symbols是无法被直接使用的。需要在Web.config中添加compilerOptions(在View目录下的Web.config添加无效),该设置同样适用于ASP.NET其它技术。假如你需要一个TEST的定义,参考如下: Web.config &lt;system.codedom&gt; &lt;compile...

weixin_33738982的博客 160

从Trace和Debug来看条件编译(Conditional Compilation

条件编译,顾名思义,就是根据在编译时指定的条件决定最后需要编译的代码。条件编译是我们可以针对某些特性的环境编写相应的代码,比如有写的代码只需要在Debug模式下才需要执行,有些代码仅仅是为了在SIT或者UAT环境下有效地进行Troubleshooting,而在Production环境下则不应该执行。通过条件编译机制,我们可以针对某中特定的“条件编译符(Conditional Compilati...

weixin_34219944的博客 97
上一篇: Using the Debug Class
下一篇: Summary
masterall
博客等级 码龄22年 17粉丝 557原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值