Is there a conditional define for library projects in Delphi?

前端 未结 2 1359
耶瑟儿~
耶瑟儿~ 2021-01-23 04:57

I have a utility unit with code shared between a few applications and DLLs. I\'d like to selectively compile portions of this unit based upon the current project type: Applicati

相关标签:
2条回答
  • 2021-01-23 05:28

    IMHO there is absolutly no need for such conditionals because of existing conventions.

    Compiling an Application or Library (the same on this compiling aspect) or a Package differs like so:

    • Application/Library will compile only the used parts from the unit
    • Package will compile all parts from the unit referenced by the interface part of the unit

    Example Unit

    unit foo;
    
    interface
    
    procedure foo1;
    procedure foo2;
    
    implementation
    
    procedure foo3; 
    begin
      // used by foo2, compile depends on foo2 compilation
    end;
    
    procedure foo4;
    begin
      // will never be compiled, because is never used
    end;
    
    procedure foo1;
    begin
      // Package: will always be compiled
      // Application/Library: will be compiled if used 
    end;
    
    procedure foo2;
    begin
      // Package: will always be compiled
      // Application/Library: will be compiled if used 
    
      foo3;
    
    end;
    
    end.
    

    That is also a reason, why using packages may result in bigger exe files, because it can contain unused code parts from precompiled dcu files.

    0 讨论(0)
  • 2021-01-23 05:41

    There is no such pre-defined conditional, and there could not be such a conditional. That's because at compilation time it is impossible to know whether the unit will, ultimately, be linked into an executable, a library or a package.

    In fact, the same compiled unit could be linked into any or all of the above project types. And indeed you can see this yourself when you link the RTL into your projects. You link the same System unit, the same compiled .dcu file, into all your projects, irrespective of the project type.

    0 讨论(0)
提交回复
热议问题