Recent

Author Topic: Best way to share units with multiple projects  (Read 668 times)

kveroneau

  • Jr. Member
  • **
  • Posts: 72
Best way to share units with multiple projects
« on: June 11, 2019, 02:43:37 am »
My initial idea is to create a Lazarus package, and place all my common units to be shared in there, then have my project have that package as a requirement.  Is this how most people share a common set of units with multiple projects?  I do not plan on making these units publicly available, they are solely created for this very specific set of projects being developed and compiled on the same machine.

I have also seen some projects, namely the few within the Lazarus source tree basically have multiple LPI and LPR files in the same directory, but with a different set of project files, and some of those projects share some of the same pascal source files.  Is this recommended, or can it cause problems with the build process, as there is still a singular lib/ directory being shared between all the projects with the compiled versions of those units.

What does everyone here normally do if they plan on creating a larger codebase with multiple output binaries and/or shared libraries which share a common set of units with required records, classes, and constants for all compiled binaries?  Should I just create a shared library at this point?  Any suggestions?

There's also a third method I forgot to mention above, in my project options, I can also set a specific file path for -Fu, and perhaps place my common set of units there to keep my overall source directory clean and tidy.  Although doing that does make managing those units a bit more difficult, as those unit files are not managed by Lazarus or my project, so editing and rebuilding them will be a bit more tedious and manual...
« Last Edit: June 11, 2019, 02:46:49 am by kveroneau »

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: Best way to share units with multiple projects
« Reply #1 on: June 11, 2019, 06:25:44 am »
Perhaps what I do is overly simplistic, and maybe not what you are asking, but I just add the needed library files and path to a project:

+ mylibrary
|
+ myproject1
|
+ myproject2
|
+ myproject3

The library includes graphics, matrix, preference, special lists, dictionaries, etc.

“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: Best way to share units with multiple projects
« Reply #2 on: June 13, 2019, 10:46:56 am »
Using the same source code files over multiple projects only really works if you're the only developer, as it is really hard to prevent someone working on Project1 to change something that breaks Project2.

kveroneau

  • Jr. Member
  • **
  • Posts: 72
Re: Best way to share units with multiple projects
« Reply #3 on: June 14, 2019, 05:15:55 pm »
I will be the sole developer of the projects which will be sharing the multiple units, so I was thinking of taking the approach originally of having a unified project directory with multiple .lpi project files, and some shared units...  However, I almost feel like this might get a bit messy after I start creating a large number of units.  As a result, I am thinking of going with the Lazarus Package approach, as it's very clean, and enforces code separation better.  It's also much more clearly visible which units are being shared between projects, so I don't break class and method signatures.

My shared units are mainly backend type code, such as shared records, classes, and method calls related to both file storage and encryption, so each project will need to know how to read/write the same set of file formats, but using said files for a different purpose.  I may look into turning the shared units into a .so/.dll depending on how large the shared code ends up being.

wp

  • Hero Member
  • *****
  • Posts: 6459
Re: Best way to share units with multiple projects
« Reply #4 on: June 14, 2019, 05:32:55 pm »
I am thinking of going with the Lazarus Package approach, as it's very clean, and enforces code separation better.  It's also much more clearly visible which units are being shared between projects, so I don't break class and method signatures.
I absolutely agree.

Please consider also that there is a common misconception that packages must be "installed" into the IDE. If a package contains only run-time code, i.e. nothing which will appear on the component palette, this is NOT true, the package only must be COMPILED so that the IDE knows where to find the compiled package files.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jshah

  • Full Member
  • ***
  • Posts: 200
Re: Best way to share units with multiple projects
« Reply #5 on: June 14, 2019, 06:09:02 pm »
I have folder called common which has units, forms used in all of my projects.

I just add the path of the common folder to my project using Project Option->Paths->Other Unit files (-Fu)

glorfin

  • Jr. Member
  • **
  • Posts: 66
Re: Best way to share units with multiple projects
« Reply #6 on: June 17, 2019, 02:11:44 pm »
I vote for package approach. First, you better see what is specific for particular project and what is shared. Second, the whole system is easier move for example to another computer with possibly different paths, because you must not modify paths for every project.