Recent

Author Topic: How to save an updated unit used in multiple projects to a different folder  (Read 521 times)

tfurnivall

  • New Member
  • *
  • Posts: 49
I have  a unit,  SDLTrace, which I use in all of my earlier projects. I'm now working on shifting my development to Lazarus, and am wondering how to implement this. I can 'use' if from its regular location (Lazarus Projects\_Components) but if I update it how can I make Lazarus save it back to that location rather than the Project location.

{Thinking about the problems I had creating it in the first place, I hope that the project is smart enough to see whence it came, and top update it automagically, but at the moment I just need to get it done, rather than experimenting}

Any pointers, advice welcome. (I don't want to add learning a VCS to the burden of learning Lazarus!)

Thanks,

Tony

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11473
  • Debugger - SynEdit - and more
    • wiki
The best way is to put such shared units into a package.

BACKUP everything first



If all units in that folder are shared...

- Menu: Package > new package
- name and save the package to the folder
- add all the shared files (from that folder) into the package.

- remove the files (and unit path) from your project
- add the package to your project.

If you have several groups of shared files in different folders, then do a package for each such folder.

----------------------

Quote
but if I update it how can I make Lazarus save it back to that location rather than the Project location.

Not understanding the question. Lazarus should save the file to where you opened it from.

That is unless you made a copy at some point, and opened that copy?

tfurnivall

  • New Member
  • *
  • Posts: 49
So, I fell back on my usual practice of creating a LAB project. It's attached and is called TestUnits.

If you open it and then open Lazarus using TestUnit.lpr, you (should) find that there are the predictable Unit1 and Form1. If you look at the Project Inspector you will see that TestUnits.lpr and unit1.pas are listed in the project.

There may be two units sysutil and lexutil. However, they don't show up in the Project Inspector. Nor (at first) are they listed in the Uses statement of unit1 (not a problem, because all that unit does is the form).

If I add symutil and lexutil to the uses statement of unit1, they still don't appear in the project inspector, but the compiler says "Can't find lexutil (and presumably symutil)".

So - interim deductions:
1)  Source is just source - not until the compiler gets its hands on the source does it hiccup because a unit is missing.
2)  The Project Inspector lists only stuff that has been added by the IDE - extending the uses statement does not affect the project information file.

I added the lexutil unit (File|Open Unit) which doesn't do anything. However, File|Open and then going to the lexutil.pas file (which is stored in the _Components directory of the Project directory) does bring in the unit. but does not add it to the Project Inspector.  Saving the project and Saving the project as itself also have no effect.

Nor does the compiler recognize the fact that lexutil is sitting there with an eager look on its face and its tongue hanging out like a labrador retriever.

Further deductions
1)  Open Unit doesn't seem to do anything (I'm certain it does something, but not visibly, with bells and/or whistles)
2) Open File will add a unit into the IDE, but doesn't add it anywhere else. It also doesn't satisfy the compiler.

Question:

How can I successfully add a Unit from a local directory of the project (I don't mind copying it in there myself, because it will need to end up there eventually)?

Where is all of this documented? - I hate having to expose my ignorance and test your patience!

Thanks,

Tony

dseligo

  • Hero Member
  • *****
  • Posts: 1533
So, I fell back on my usual practice of creating a LAB project. It's attached and is called TestUnits.

There is nothing attached.

tfurnivall

  • New Member
  • *
  • Posts: 49
My bad!
Here it is...

n7800

  • Sr. Member
  • ****
  • Posts: 400
When searching for units (in the IDE and when compiling), the search paths specified in the Project Options are taken into account: "Main Menu > Project > Project Options > Compiler Options > Paths". Add the path to your "_Components" folder in the "Other unit files (-Fu)" field. I'll attach a screenshot for clarity.
* You can specify as many paths as you like, separated by a semicolon ";".
* The path can be relative (including parent folders "..\").

"File > Open Unit" simply opens the file for editing. You can simply drag it into the Source Editor or just use the universal "File > Open ... [Ctrl+O]". This does not add the file to the project or search paths. Therefore, you can view and edit files that are not related to your current work at all.



Another way to add the current file to the project is to use "Project > Add Editor File to Project". You should be asked to add a search path, agree. Then the folder containing this file will be automatically added to the Project Options (which I showed above).

It is logical that other files from this folder do not need to be added, since the search path has already been added.

In fact, "project files" (from the Project Inspector) and "search paths" (from Project Options) are different things, but I will not go into details...

If you are interested in how to optimally use the same units in different projects (without copying them), then I can give a couple of tips (in addition to the already mentioned option of adding to the package).

tfurnivall

  • New Member
  • *
  • Posts: 49
Quote
If you are interested in how to optimally use the same units in different projects (without copying them), then I can give a couple of tips (in addition to the already mentioned option of adding to the package).

Yes please! My choice of Lazarus was predicated on being able to share code - executable as well as source. My big disappointment with VBA (from the time I started using it "some time" ago) was the monolithic nature of the compiled object code. (But then Windows and DLL hell have never been very exciting to contemplate).

I'm looking for true Compile once, Share anywhere.

Tony

n7800

  • Sr. Member
  • ****
  • Posts: 400
For example, you can specify a relative path to "..\_Components" in the search paths:

Code: [Select]
Lazarus Projects
    _Components
        myunit1.pas
        myunit2.pas
    project1
        project1.lpi
        ...
    project2
        project2.lpi
        ...

Or you can use symbolic links. Place your "_Components" folder anywhere, and add a subfolder to each project that is a symbolic link to it. Then you will need to specify the name of this subfolder "_Components" in the search paths.

Code: [Select]
<path>\_Components
    myunit1.pas
    myunit2.pas
Lazarus Projects
    project1
        _Components --> <path>\_Components
        project1.lpi
        ...
    project2
        _Components --> <path>\_Components
        project2.lpi
        ...

Or you can even use IDE macros and environment variables. Then you need to specify "$env(mylib)" as the search path. To do this, you will need to declare the environment variable "SET mylib=path-to-mylib" in the system, or call the IDE each time with the script "SET mylib=path-to-mylib && lazarus".

Of course, you can use packages, as suggested, or come up with something else...



Which of these is more portable (and which is harder to update when moving a project/library/IDE/PC) is a complex question, depending on your needs and knowledge (and your potential users). It also depends on the number of files in your "library", the presence of a repository in it (it is better to have one repository in a package, and not nested in each project), the number of Lazarus installations, whether you want to publish the project, and much more...

 

TinyPortal © 2005-2018