Recent

Author Topic: Determine if project is using 3rd party units?  (Read 2516 times)

knuckles

  • Full Member
  • ***
  • Posts: 122
Determine if project is using 3rd party units?
« on: March 20, 2018, 04:49:40 pm »
I am making my own reusable library and in one of the units I want include extra methods and functions etc to support lcl/vcl more, but I also want to include other 3rd party controls such as SynEdit.

Lets say you have a uses clause like so,

Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2.   Windows,
  3.   SysUtils,
  4.   Classes;
  5. {$ENDIF}

If I were to add SynEdit above, create a new project that uses the above unit this would compile under Lazarus since it's pre installed, but in Delphi it would error because by default Delphi has no knowledge of SynEdit.

I think I need defines, so for example in my unit above Im thinking I could use something like,

Code: Pascal  [Select][+][-]
  1. {$IFDEF SYNEDIT}
  2.   SynEdit;
  3. {$ENDIF}

And whenever I create a new project that uses this unit, I could write {$DEFINE SYNEDIT} from the project that uses my unit and that would tell my unit that the project also wants access to the extra synedit related methods and functions I could code.

It never gets triggered though, I could write anything in place of SynEdit; and the compiler never complains, likely because it never gets past {$IFDEF SYNEDIT}

Hope this makes sense, basically I want to know how I can let any project, if it defines {$DEFINE SYNEDIT} to let it access those extra methods and functions from my unit, I could wrap them in {$IFDEF SYNEDIT} for example, this way the compiler will never throw an error.

Another way would be to somehow determine if the units exists in the library path but I am unsure, I think my basic understanding of defines is probably the way to do it but Im possibly doing something wrong.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Determine if project is using 3rd party units?
« Reply #1 on: March 20, 2018, 05:06:59 pm »
Whether you compile your project with FPC or the Delphi compiler, if you use TSynEdit in your project you need to include SynEdit in the uses clause. No define is needed for that.

The difference is that if you develop using Lazarus, the Lazarus IDE knows where to find synedit.ppu already.
Whereas if you compile using the Delphi IDE you need to tell Delphi (perhaps by putting synedit.dcu in the library path, or in some other way) where it can find the library (or if you want to compile it from scratch, where synedit sources are).

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Determine if project is using 3rd party units?
« Reply #2 on: March 20, 2018, 05:17:44 pm »
Synedit as maintained by Lazarus maintainers is NOT third party ......
« Last Edit: March 20, 2018, 05:20:36 pm by Thaddy »
Specialize a type, not a var.

knuckles

  • Full Member
  • ***
  • Posts: 122
Re: Determine if project is using 3rd party units?
« Reply #3 on: March 20, 2018, 10:05:38 pm »
Ok maybe my question is not been fully understood, also synedit was purely an example, it could in theory be any unit from any 3rd party that is not part of lazarus/delphi.

I just want to know a way to tell any project that here we are going to be using xxx 3rd party and so we will define it, and then my unit will check if such a define has been made and then allow access to it.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Determine if project is using 3rd party units?
« Reply #4 on: March 20, 2018, 10:56:52 pm »
In Lazarus projects the way to specify dependency on code provided by a third-party is to add the third-party's package as a dependency (using the Project Inspector). I don't see why you insist on needing to insert a {$Define...} to do this.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: Determine if project is using 3rd party units?
« Reply #5 on: March 20, 2018, 11:19:13 pm »
I am not sure if a "{$Define" in one unit, can ever affect on $Ifdef in an other... You can give the compiler option -dSYNEDIT, but even then you have to understand the order in which this takes affect.

1) A DEFINE/IFDEF only works when the unit is compiled, but never when it is used.
More precise, it takes effect, when the *.pas code is translated into *.ppu/*.o files. ppo and o files do not contain ifdef, the code has already been filtered.

2) When you write "uses foo;", then foo, is first translated to a ppu, and the ppu is used.
If foo needs to know SynEdit was defined then use the commandline option -d.
I am not sure, but I think any $Define in the unit that uses foo has no effect.

3) When you use packages.
The package (your package), is compiled before the project.
Whatever happens in the project (even commandline switches) will not affect what had already happened in the package.

You have to change the commandline switches for compiling the package, then it will be recompiled. In Lazarus you can use "additions and overrides" to do this from the project.

-------------------
Best solution imho
Provide 2 packages. One without SynEdit, The other with extra units providing the extras for SynEdit.

The 2nd package uses the 1st package as a normal package. No IFDEF at all.

Do NOT:
- play with include path or any path settings at all (it will come haunting you)
- mix 2 packages into one folder (Rather put each package into a separate subfolder

This will probably take some redesign, moving stuff to base classes, adding new inheritance, whatever....

knuckles

  • Full Member
  • ***
  • Posts: 122
Re: Determine if project is using 3rd party units?
« Reply #6 on: March 21, 2018, 08:54:23 pm »
Thanks for the insight Martin, i'll consider what you have said

 

TinyPortal © 2005-2018