Recent

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

kveroneau

  • Full Member
  • ***
  • Posts: 119
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: 1215
  • 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

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
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

  • Full Member
  • ***
  • Posts: 119
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: 11854
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.

heejit

  • Full Member
  • ***
  • Posts: 245
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

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
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.

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Best way to share units with multiple projects
« Reply #7 on: February 08, 2021, 07:56:20 pm »
What is the best way to have a set of commands and functions that all projects will use, but that could be from multiple clients and therefore needs to be separated and protected?

My idea is to create libraries (.dll files) of all needed commands and functions, but I am having a heck of a time making them work.  I have them working in the project and .pas files.  But it is time to move them to a central location so all other .pas files in multiple projects can use these same functions and commands.

All help is appreciated.

Mike

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Best way to share units with multiple projects
« Reply #8 on: February 08, 2021, 09:33:23 pm »
This is an old thread, and I now use a package to contain my library of shared units, following the suggestions here. The package can be compiled on each platform.

Would this work for you?
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Best way to share units with multiple projects
« Reply #9 on: February 09, 2021, 12:18:22 am »
This is an old thread, and I now use a package to contain my library of shared units, following the suggestions here. The package can be compiled on each platform.

Would this work for you?

Are there any video or web tutorials on creating packages in the way you describe?  Not sure is packages will work.  It will have to be useful for cross deployment, such as Delphi, Lazarus and on Mac, PC, Linux and eventually online/mobile.

Thank you for your help.

Mike

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Best way to share units with multiple projects
« Reply #10 on: April 02, 2021, 10:27:28 pm »
This is an old thread, and I now use a package to contain my library of shared units, following the suggestions here. The package can be compiled on each platform.

Would this work for you?

Yes, the package approach would work.  I will look up how to create a package, but any help is appreciated to expedite the process.

Mike

cdbc

  • Hero Member
  • *****
  • Posts: 1025
    • http://www.cdbc.dk
Re: Best way to share units with multiple projects
« Reply #11 on: April 03, 2021, 09:21:21 pm »
Hi
Have a look at the FPC rtl...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Best way to share units with multiple projects
« Reply #12 on: April 03, 2021, 11:24:20 pm »
The 'Package' menu in Lazarus has most of what you need.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

 

TinyPortal © 2005-2018