Recent

Author Topic: Reusable personal code libraries  (Read 1973 times)

rwebb616

  • Full Member
  • ***
  • Posts: 133
Reusable personal code libraries
« on: May 11, 2021, 06:43:11 pm »
I'm fairly new to Lazarus and learning.  I'm wondering how to best set up my own personal library of units that I can use for multiple applications for instance let's say I have some db routines that I write that would be the same for multiple applications.  What would be the best way to set that up? 

Rich

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Reusable personal code libraries
« Reply #1 on: May 11, 2021, 06:50:55 pm »
Create a new package (menu package -> new package).

Then in the package (the package window), in the toolbar "Add" > New File.

Now when you add the package to your project, you can add any of it's units to your "uses" clause.


You may find other methods on the internet, involving editing Path settings. Yes that is possible, but it can really quickly get you very strange errors.
Modifying the Path settings requires in depth knowledge.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Reusable personal code libraries
« Reply #2 on: May 11, 2021, 06:55:17 pm »
Also, which ever way you go: Create yourself a repository (e.g. local git) of your units.

So if you edit them for project A, and you later compile project B and find your edits where wrong => you can still recover the old versions.

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Reusable personal code libraries
« Reply #3 on: May 11, 2021, 06:56:29 pm »
Create a new package (menu package -> new package).

Then in the package (the package window), in the toolbar "Add" > New File.

Now when you add the package to your project, you can add any of it's units to your "uses" clause.


You may find other methods on the internet, involving editing Path settings. Yes that is possible, but it can really quickly get you very strange errors.
Modifying the Path settings requires in depth knowledge.

That was a fast answer!  And it seems relatively simple. 

This forum is really amazing.  There are a handful (or three) of dedicated people that are always responding and to them I say THANK YOU!  Thank you for sharing your knowledge and your time to post examples and help us newcomers feel welcome and at the same time learn something!  I hope at some point that I can get good enough where I could actually answer some questions. 

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Reusable personal code libraries
« Reply #4 on: May 11, 2021, 07:01:30 pm »
Also, which ever way you go: Create yourself a repository (e.g. local git) of your units.

So if you edit them for project A, and you later compile project B and find your edits where wrong => you can still recover the old versions.

I'm still learning how to use Git.  Probably would be a good investment of time to get skilled in it's use.  I also have my own Atlassian BitBucket server which I guess could serve me well. 

Are there any IDE integrated git tools?  I used I think it was VS-Code which had git all integrated and you could make a commit? I think it's called... right from the editor. 

Rich

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Reusable personal code libraries
« Reply #5 on: May 11, 2021, 09:26:14 pm »
I was going to suggest a local Subversion repository, but held off pending a comment from somebody who knew what he was talking about (Martin in this case :-) However TBH I prefer Subversion to Git: if you don't need collaborative facilities then being able to say "revision 123" without ambiguity is extremely useful.

I can't speak for installing a local Git, but a Subversion server builds easily PROVIDED that you only want the standalone svn protocol. There certainly used to be a Subversion plugin for the Lazarus IDE, but I'm not really sure what it brought to the party /unless/ it sorted out the slightly ungainly situation when one tries to rename a unit and finds it needs to be changed in both the IDE and Subversion.

Noting Martin's comment, I've seen the IDE get confused when debugging a project spread over multiple directories, but I've never explored whether that's sorted out by using a package. I would say however that on occasion one doesn't want to bundle a whole lot of files together: I'm thinking in particular of the situation when one uploads a project to Github/Sourceforge in which case the less cruft that's bundled the better.

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Reusable personal code libraries
« Reply #6 on: May 11, 2021, 10:45:34 pm »
Both git/svn are valid.

For a local repro, git does not need a server. (Yet if you want to sync with your laptop, any git client dir can act as server dir / using shared folders or git-server).
On Windows all you need is the tortoise client.

If you go for git, make sure you use "git switch" and "git restore" instead of "git checkout".

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Reusable personal code libraries
« Reply #7 on: May 11, 2021, 10:56:16 pm »
I don't think a Subversion server works with file shares etc., but it can be built without the massive overhead (and complexity) of Apache... I'm sure I stuffed that in the wiki somewhere but I'm blowed if I can find it :-/

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Reusable personal code libraries
« Reply #8 on: May 11, 2021, 10:56:27 pm »
Noting Martin's comment, I've seen the IDE get confused when debugging a project spread over multiple directories, but I've never explored whether that's sorted out by using a package.

Packages is like the LCL, and debugging into the LCL has never caused me an issue.

However, you do need to enable debug info for the package.


I know of one issue that can confuse the IDE. That is if your folder is also available via symlinks. For the IDE the symlink folder is a distinct location. So the file exists twice.

If your package (or any other method) says the file is in
 /home/foo/bar/unit.pas
Then codetools, the debugger (via fpc) and the IDE will want that file.

If you have a symlink
 /home/foo/linkedbar/unit.pas
And you use menu File > open to open it from that folder, the IDE thinks (mostly) it is a different file. And that will cause trouble.

But this is the same with package, or amending pathes directly, or using any other method of locating files.
This is even the case for a project with all files in a single folder.



wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Reusable personal code libraries
« Reply #9 on: May 11, 2021, 11:11:10 pm »
I'm fairly new to Lazarus and learning.
I don't know what this means exactly. Unlike the others my answer will focus on the "new". Because if you reall are new to programming I'd recommend to keep your hands off of git and svn and these things at first which can be nightmares of their own and you will have your head full of issues with Lazarus already.

To learn how to share code between projects you should take small steps. The first one certainly is that you must learn how to extract the potentially commonly used code into a separate unit (or units). In the next project simply copy this unit into the project folder and try to call these exported procedures - only if you had been able to extract the code such that it does not have any dependence on the first project this will be successful.

Then, when you found this to work, create a package and add these shared units to the package, as Martin has told you. Make a copy of your old projects and delete the shared units from the project folder, but add the package to the requirements of the projects - this way you can access the shared units without explicitly specifying where they are.

When all this works you did great progress. Only now - after you mastered the most part of the Lazarus/FPC basics - you can think of learning how to work with version control systems (git, svn, hg)...

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Reusable personal code libraries
« Reply #10 on: May 11, 2021, 11:34:43 pm »
Good advice all, I have followed wp's approach.

I have been actively using Lazarus for over 9 years and am still learning. I currently maintain 3 large scientific projects that share a library with graphics, math, and general routines. For many years I simply added the paths of all the library directories to the project path.

This is very simple, and you can start there, but it started to get unwieldy with multiple projects on three platforms, so I started experimenting with packages. This is more effective, but I still get confused occasionally.

I have only in the last few years begun to use git, which is great but has a learning curve. I am moving towards open source so it is critical. Prior to that I simply zipped a project before making changes, applying a standard versioning number.

So, in my case, I have been incrementally improving my organizational skills, rather than jumping in with both feet.
« Last Edit: May 12, 2021, 12:14:23 am by VTwin »
“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)

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Reusable personal code libraries
« Reply #11 on: May 11, 2021, 11:52:19 pm »
I'm fairly new to Lazarus and learning.
I don't know what this means exactly. Unlike the others my answer will focus on the "new". Because if you reall are new to programming I'd recommend to keep your hands off of git and svn and these things at first which can be nightmares of their own and you will have your head full of issues with Lazarus already.

To learn how to share code between projects you should take small steps. The first one certainly is that you must learn how to extract the potentially commonly used code into a separate unit (or units). In the next project simply copy this unit into the project folder and try to call these exported procedures - only if you had been able to extract the code such that it does not have any dependence on the first project this will be successful.

Then, when you found this to work, create a package and add these shared units to the package, as Martin has told you. Make a copy of your old projects and delete the shared units from the project folder, but add the package to the requirements of the projects - this way you can access the shared units without explicitly specifying where they are.

When all this works you did great progress. Only now - after you mastered the most part of the Lazarus/FPC basics - you can think of learning how to work with version control systems (git, svn, hg)...

Thank you for that bit of advice.  I'm not new exactly to programming - I've dabbled a little bit in VB.NET, done some access programming, some PHP etc.  So I understand the basics.  I've also worked a tiny bit with Git before in VS-Code when I was working on some php stuff but really only within the confines of the features of the IDE.  I'm newer to Lazarus than anything - Pascal is not entirely new to me as I took a class on it in college and I absolutely loved the language - it was my favorite programming class out of all of them.  For my career I am an IT consultant so I'm technical minded but I got burned out with programming in .net.  It always seemed like just about the time I was learning something Microsoft changed how they were doing this - specifically data access - originally with VB then .net brought it's new additions, then lync and entities .. finally I just said I'm done.  Then I learned about Lazarus and Delphi and decided to start learning and that's where I'm at now.

I have successfully written what I feel is a somewhat advanced project - a computer (and team) version of Ellen DeGeneres's Heads Up! game.  It is different in that two to four teams of people can play and the words are displayed on a TV.  We are doing a test run of the game tonight in fact at our youth group to see how it goes. 

Hope that gives more of an idea of my current status.
Rich

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: Reusable personal code libraries
« Reply #12 on: May 12, 2021, 02:57:35 am »
I've also worked a tiny bit with Git before in VS-Code when I was working on some php stuff but really only within the confines of the features of the IDE.

For Git, I suggest that you get familiar with the handful of basic commands from the CLI - add, status, commit, push, etc - then look into IDE integration. This is so that, in case you hit IDE-Git integration bugs, it is easier to figure out what went wrong and how to recover using the CLI because you are already familiar with what Git is doing.

When you want to go beyond Git on a single computer, you could use Github or a lookalike. I use a Gitea https://gitea.io/ running on a Raspberry Pi at home. It comes as a single executable.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Reusable personal code libraries
« Reply #13 on: May 12, 2021, 06:19:11 am »
I'm Win-style programmer and I'm from that time, when disk/RAM consumption mattered. So, not only sources, but binaries should have been reusable. Concept of "solid" applications, i.e. applications, that always include it's whole code, because one always has all sources, is new for me. I need to get used to it. So, I would suggest making DLL or SO.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Reusable personal code libraries
« Reply #14 on: May 12, 2021, 05:03:44 pm »
(Subversion) can be built without the massive overhead (and complexity) of Apache... I'm sure I stuffed that in the wiki somewhere but I'm blowed if I can find it :-/

The version I've used to good effect for an extended period is 1.6.16, the source comes in two files from https://archive.apache.org/dist/subversion/ with minimal external dependencies and if it doesn't find a suitable database it uses its own "FSFS" backend. Unfortunately the SSL API has changed since I last built it, and so far I don't see an easy workaround.

It does appear that at least some OSes bundle svnserve and svnadmin in with the client utilities, and that they can be started up with a bit of systemd arcana.

MarkMLl

MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018