5

I have many Delphi 10 projects that are using the same units, let's call them "commons".

When I add anew unit to commons, I have to manually add it to each project. I have tried adding a {$INCLUDE commons.inc} line into the uses part of each .dpr file:

uses
    Forms,
    {$INCLUDE commons.inc}
    projectUnit1,
    ...;

commons.inc has this content:

common1,
common2,

I can compile a project but cannot manage the units from commons.inc. By manage, I mean Ctrl-F12, remove from project, etc.

This is from Delphi's help:

There is one restriction to the use of include files: an include file can't be specified in the middle of a statement part. In fact, all statements between the begin and end of a statement part must exist in the same source file.

I suppose that is why my idea does not work?

Am I doing something wrong, or is there another solution?

Daniel Vidić
  • 164
  • 2
  • 11
  • 1
    Sure that you can compile without errors? IMO at least the include should read `common1, common2,`. The `;` would terminate the uses statement. – gammatester May 24 '18 at 17:30
  • 1
    The IDE basically "owns" most of the DPR file. But as a side note, should you use `,` instead of `;` inside this inc file? – Jerry Dodge May 24 '18 at 17:31
  • Outside the IDE, if you change `common1; common2;` into `common1, common2,`, it probably compiles. But inside the IDE, you'll get problems. The IDE tends to remove such $INCLUDEs, because it *owns* the DPR file. A DPR file is not exactly like a normal PAS file. – Rudy Velthuis May 24 '18 at 17:57
  • I have used ',', not ';' and corrected question. – Daniel Vidić May 24 '18 at 18:19
  • 1
    I haven't personally tried this but try creating a pre-build event script to do a search/replace inside the dpr file. Pre-build events accept DOS commands. My guess is call a bat file that calls a powershell command to search/replace inside the dpr file. http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Creating_Build_Events https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Sam M May 24 '18 at 18:45
  • 5
    Yup, this is to be expected. IDE won't read in to your include files. That's just the price you pay. – David Heffernan May 24 '18 at 19:46
  • 1
    You can add the two files to your .dpr using Project->Add to Project and multi-selecting just as fast as you can type in the $INCLUDE statement, and then you can manage them via the IDE just as you're complaining about not being able to do. This sounds like an XY problem - *I'm trying to come up with a way to kludge around problem Y that I caused myself first by not using X properly in the first place* - to me. – Ken White May 24 '18 at 22:53
  • 1
    @Ken White I belive you are missing the point, I have multiple (around 30) projects and when there is new commonX file I have to add it to each project and that is 30 times Project->Add. My question is to find a way to do it only once. – Daniel Vidić May 25 '18 at 06:22
  • 1
    You have to make a choice. If you want the IDE to be able to work with the dpr file you can't use include files in it like that. If you are happy to sacrifice that convenience fine. Otherwise don't use include files. In which case write simple tool using your favourite scripting language to add new files to each of your projects. – David Heffernan May 25 '18 at 06:27
  • According to Sam M and David Heffernan comments it is obviously I have to go with a scripting solution. Thanks. – Daniel Vidić May 25 '18 at 06:36
  • 1
    Pre build event is a really bad idea. – David Heffernan May 25 '18 at 07:02
  • I tend to migrate all the application initialization to my own procedure in its own unit, usually named `RunApp`. The main DPR just calls that universal procedure. This way, I have full control. On the downside, the IDE then has no idea what type of project you have, and disables features. So I have to compromise. But it's worth it to take control over your app's internals. – Jerry Dodge May 26 '18 at 03:04

1 Answers1

1

This workaround might suit. The only downside I have found so far is that the included files do not appear in the Project Manager.

  • Add the folder(s) containing the files to be included to the search path of every project.
  • Create Include.pas, a normal .pas file, and include it in the normal way in every project.
  • Add the files to be included in multiple projects to the uses clause of Include.pas. $IFDEFS can be used if required.
  • Didnt get it at first, but yes, this might work. May start with "The workaround described below might suit:" which makes the answer more readable. I initialilly thought you were referenceing one of the earlier comments. – H.Hasenack Jan 17 '19 at 21:31