取决于特定项目,公共单元中的条件编译? [英] Conditional Compilation in common unit depending from specific project?

查看:77
本文介绍了取决于特定项目,公共单元中的条件编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi XE2中,我有一个单元 MyUnit.pas ,供两个不同的项目 ProjectA ProjectB 使用.
MyUnit 包含语句 DoSomething; (这是在另一个单元 OtherUnit.pas 中实现的过程).
现在,我想使用条件编译仅在 ProjectA 编译中包含 DoSomething ,而在 ProjectB 编译,以免 ProjectB 间接包含/编译 OtherUnit.pas .
此必须为有条件的编译,因为简单的 if/else 语句显然不适用于此目的.
如何实现?

In Delphi XE2, I have a unit MyUnit.pas which is used by two different projects ProjectA and ProjectB.
MyUnit contains a statement DoSomething; (which is a procedure implemented in an other unit OtherUnit.pas).
Now I want to use Conditional Compilation to include DoSomething only in ProjectA compilation and not in ProjectB compilation, so to avoid ProjectB including/compiling OtherUnit.pas indirectly.
This MUST be Conditional Compilation, as a simple if/else statement obviously does not work for this purpose.
How can this be achieved?

推荐答案

您需要在一个项目中定义一个条件,而在另一个项目中没有定义.例如,您可以在项目A的项目选项中定义 CanUseOtherUnit ,而不是项目B.

You need to define a conditional in one project, but not the other. For instance, you might define CanUseOtherUnit in the project options for project A, but not for project B.

然后,您需要对 MyUnit.pas 进行以下更改.

Then you need to make the following changes to MyUnit.pas.

将引用 OtherUnit uses 子句放在 $ IFDEF 中:

uses
  ... {$IFDEF CanUseOtherUnit}, OtherUnit{$ENDIF};

然后在调用函数的位置,再次将调用包装在 $ IFDEF 中:

And then at the point where you call the function, again wrap the call inside an $IFDEF:

{$IFDEF CanUseOtherUnit}
DoSomething;
{$ENDIF}

由于没有在项目B中定义条件,编译器将忽略 $ IFDEF 指令中的代码.

Because the conditional is not defined in project B the compiler ignores the code inside the $IFDEF directives.

当您主动希望不使用某个装置时,搜索路径的便利性将成为一个弱点.您很容易在没有意识到的情况下将单元添加到程序中.如果您不使用搜索路径,而被迫将源文件添加到项目(.dpr文件)中,那么您就不会无意间采用新的依赖关系.

When you actively desire for a unit not to be used, the convenience of search paths becomes a weakness. It's just too easy for you to add units to the program without realising it. When you do not use search paths, and are compelled to add the source files to the project (.dpr file) then you cannot accidentally take a new dependency.

这篇关于取决于特定项目,公共单元中的条件编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆