HOME - - - - - Lazarus Tutorials TOC - - - - - - Other material for programmers
Delicious.Com  Bookmark this on Delicious     StumbleUpon.Com Recommend to StumbleUpon

Efficient Lazarus Development

Re-using the code of an earlier project

This has good information, and a search button at the bottom of the page

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

This page is "browser friendly". Make your browser window as wide as you want it. The text will flow nicely for you. It is easier to read in a narrow window. With most browsers, pressing plus, minus or zero while the control key (ctrl) is held down will change the texts size. (Enlarge, reduce, restore to default, respectively.) (This is more fully explained, and there's another tip, at my Power Browsing page.)

Sometimes the project you are planning to write today has much in common with a project you wrote previously.

It is easy to use an earlier project as the basis for a new one. You could probably figure everything out for yourself. But if you'd rather, read on, and take advantage of the fact that I've written out the steps for you.

Some of the advice below assumes you want to use my working practices. In particular, the names of folders, files, objects do not have to follow the guidelines I offer.

How to clone a project

First, shut down the Lazarus IDE, if it happens to be running at the moment.

Then create new folder. For this example, I am going to show you how to create an application called LDN004 from one called LDN003, so the folder name would be LDN004.

Use your operating system's usual tools, copy all parts of source project, to the new folder. (The "backup" and "lib" folders do not need to be copied, nor the .exe file, but if it is just easiest to copy everything, it won't hurt. You'll be deleting the copies in a moment, anyway.)

Take care that you do indeed merely COPY the parts. (It is easy to accidentally move instead... thus eliminating your copy of the source code, etc... not good... for the application you thought you were creating the clone of.)

All of the rest of this is done in the new folder.

(Ummm.. Don't you just hate things like this? I was using my own webpage, to remind me exactly what I needed to do, July 15. The following paragraph, at that time, mentioned only an .lpr file. Hazy memories suggest that .lpr was the old .lpi. Sorry. (In July 15, I was using Lazarus v1.0 on a Windows 7 PC.)
Jan 17, Lazarus 1.6, on Win XP... cloning a Lazarus program from... ummm not sure... to 1.6/ XP environment:
"Old" project had .lpi (and .lps) but no .lpr.
Used .lpi to open clone, as per message in bold which follows.
Tested and tweaked these instructions again, 2/17, Lazarus 1.6.2, Win7... Went well! Less old project. Only had .lpi. Used that.)

If the project has an .lpi file use that to open the copy of the project for the purposes this tutorial addresses.

If you accidentally launch the programming process by double clicking the .lpr file, you risk All Sorts Of Weird Stuff. Weird stuff which can "back annotate" (change) the "original", "main", copy of the code you are creating a new app from... I think. At least something like that has happened to me more than once, and it gets Very Messy! (It would be a good idea to do a temporary (or better) backup copy of the stuff you are working from before you try to create the derivative app. Just in case.)

Assuming that the Lazarus IDE is still not running... (close it if it is!)...

1) While being careful that you are working with the copy in the LDN004 (or whatever you've called the new app) folder, double-click on the .lpi file. (Or .lpr file, if you don't have an .lpi file. ) That would be LDN003.lpi (or LDN003.lpr) in my example. Lazarus should open.

1b) Open the main unit of the project, the ".pas" file, if it isn't open already.

1c) It would be best of you were trying to clone something that will at least compile? Use Run/Quick Compile to make sure. It doesn't matter if you get WARNINGS. It doesn't matter if the code doesn't quite do what you want it to... but if it won't compile, the following may not work for you. But it might... press on, if you for some reason don't want to "touch" the project you are cloning.

2) Use the Object Inspector to change the name of the form, e.g. LDN003f1 to LDN004f1. Be sure to move the insertion point out of the form name edit box, to make your intended change happen.

3) Switch back to the window for the main UNIT, the .pas file. Click "File | Save As", and save the unit. (you should be offered .pas as the default extension. If you get .frm, you still have the focus on the unit's form, probably from when you changed its name. Be sure you are in the right folder... you should be.... Save the unit (i.e. the .pas file) under the new name. (E.g. ldn004.pas.) You may get a message asking if you want to delete the old unit. Look closely at information in the question, and as long as it is specifying the copy in your new folder, say "yes". (Say no, if in doubt, and delete the copy-in-new-folder of the old .pas file by hand, later.)

You may also be told/ asked "The unit (xyz) is used by other files. Update references automatically?". Say "Yes.)

Here's another puzzle from my July 15 session. Why, when I originally wrote the tutorial, did I write "If you don't use upper case letters in the file name, Lazarus won't complain."? (It may be that Lazarus once was... and maybe still is... fussy about filenames, doesn't like upper case in them. (Doesn't like hyphens in some contexts, I believe) At 7/15, I tried using upper case letters in the name. It didn't seem to lead to any hassles. (It IS a case-sensitive environment, but it seems to have relaxed the rules about what case is allowed in unit names. I may be creating problems for myself if I try to port a program to Linux, though. Sigh.)

4) If you created sub-units, to "divide and conquer" your code, the for their names (and the names of their forms, when present... not every sub-unit entails a form)...

If you (did that)... then go through all of them, renaming the form and then switching back to the unit, and re-saving it with a revised name. E.g., if there was, say, a sub-unit called LDN003_DrawGraph.pas, you would make that LDN004_DrawGraph.

(If the sub-units are generic, "toolkits" you use across various projects, then all will be well, if you just leave the old names alone. But if a sub-unit has "LDN003" in the name, it is an invitation to confusion to use it under that name in LDN004!)

So far, so good, but you need to save the project specification anew as well, not just the .pas unit, which you just did. (You have also already saved the definition of the form used by the .pas unit.)

5) To save the project specification (with the new base name), use the "Project" menu item. "Project | Save Project As...". The dialog will suggest that you are saving an .lpi file, but several other files, (including (perhaps) the wretched .lpr file,) arise along the way. Lazarus doesn't mind if you want to use upper case letters here.

Almost done!

Test whether you succeeded

Now, to see if you were successful....

If the cloned project starts up okay, then all is probably well! But don't run it until you read the next paragraph!

If you use ini files, go into the code, change the name as necessary. (E.g., for our example, re-code the program to look for LDN004ini.txt rather than LDN003ini.txt)

Before too long, you should delete that "Scraps" folder. Better to discover sooner rather than later that something inside it is somehow being used! (Maybe just rename it, first, if you are worried. Sometimes the operating system will thwart you attempt to hide the files you hope you aren't using... but it would be unlikely to happen.)

Final tidies

What Lazarus has taken care of for you is very impressive. It can be forgiven, if a few little odds and ends need to be dealt with.

Use the Lazarus "Search | Find" tool to scan your code for any remaining vestiges of the old application. You may have used the application's name in a comment... which will have to be edited by hand... or, say, in the text of a label, or elsewhere. Again, that will have to be changed to the new name by hand.

A productivity enhancing skill

I hope you will remember that procedure... many times a "new" application can be derived from earlier work. Be a little cautious about using earlier work which wasn't done well... sometimes a fresh start gets you away from corners you painted yourself into previously. However, the more you write applications, the better your code will be, and the more often you will be doing something which can benefit from the work done on an earlier project.

An unrelated skill... creating units to be used by many programs... will prove even more useful, once you are working on things that have common elements. I haven't explored creating stand-alone units in Lazarus yet, but I will be greatly surprised if that element of Delphi (and most other general languages) isn't supported. I have a Delphi tutorial on the subject which you may find gives you sufficient help, if you are in a hurry to study stand alone units.

            powered by FreeFind
  Site search Web search
Site Map    What's New    Search This search merely looks for the words you enter. It won't answer "Where can I download InpOut32?"

If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service. They offer things for the beginner and the corporation.www.1and1.com icon

Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Link to Lazarus Tutorials main page
How to contact the editor of this page, Tom Boyd

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....