7

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.

Cesar Romero
  • 4,027
  • 1
  • 25
  • 44

3 Answers3

11

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
6

You can't determine this at compile time, but at runtime, you can check the SysInit.ModuleIsLib (Delphi 2007) to determine if the code is running in a library (or package).

TLama
  • 75,147
  • 17
  • 214
  • 392
HeartWare
  • 7,464
  • 2
  • 26
  • 30
  • For the same of correctness, `System.IsLibrary` is what you need to detect a DLL at runtime. But the question specifically asked about compile time detection in which case none of these variables are appropriate. – David Heffernan Oct 28 '12 at 22:38
1

Best thing I can think of is to set a define in an include file. You could use a pre-build action (bat file) to modify the include file.

Remko
  • 7,214
  • 2
  • 32
  • 52