Search code examples
delphiconditional-compilationdelphi-units

Check at compile time if a unit exists


I want to use a unit if it exists. Is there something like {$IF Declared(MyUnit)} for units, or a different way?

I installed a demo version of a component package, and would like to conditionally use units from it, and conditionally add menu items for testing its functionality. All without forcing everybody to install the demo package.


Solution

  • This isn't possible. You would need there to be a condition statement that performed search for a unit. Looking in the units listed in the project file, and then the search paths. However, there is no such conditional.

    The best you can do is to use these units only if a conditional symbol is defined. You would then need to define that symbol in your project.

    This is obviously inconvenient but it's only necessary while you evaluate the component. Once the evaluation is complete you will adopt it universally, or not at all. In the meantime you will have to live with a degree of awkwardness.