Search code examples
delphicompiler-directives

How can I tell at compile time whether the project is a program or a library?


I'm trying to know if the project is a library or not, after read the help I wrote this code that does not work:

{$IF DEFINED(LIBPREFIX)}
    {$DEFINE PROJECT_IS_EXECUTABLE}
    {$UNDEF PROJECT_IS_LIBRARY}
{$ELSE}
    {$DEFINE PROJECT_IS_EXECUTABLE}
    {$UNDEF PROJECT_IS_LIBRARY}
{$IFEND}

I tried DEFINED, DECLARED and

{$IF (LIBPREFIX = '')}

Every try always returns the same for DLLs and for programs. How can I do this using only built-in compiler directives?

EDIT

My intention is to remove the extra information from "PE File".

I do it directly in .dpr project file, so no matter how the other units were compiled, but I can not do the same in DLL projects.

Therefore I was looking a way to block it in DLL projects.

This is how I solved this issue, I add this directives to my .dpr programs:

  {$DEFINE STRIPE_PE_INFO}
  {$DEFINE STRIPE_RTTI}
  {$I DDC_STRIP.inc}

And DDC_STRIP.inc has all the logic.


Solution

  • There's no way to know this when your file is being compiled. A source file can be compiled to a .dcu and then linked into any type of project. A good example are the RTL and VCL units.

    Probably the best you can do is to define a conditional in your project options that indicates whether or not the project is a library. But you need to make sure that the .dcu is always re-compiled when you build any project that uses this unit.