Forum > General
Order of execution of initialization sections
(1/1)
jwdietrich:
According to FPC's documentation of units, "the initialization parts of the units are executed in the order that the compiler loaded the units when compiling a program". But what determines this order? Is it the order, in which the units are listed in the units section of the main program (or the .lpr file, if Lazarus is used)?
In the Lazarus IDE, the units are also listed in the XML-encoded .lps file. Is it also used for determining the order of the initialization section? And, if so, what takes precedence, the units section in the .lpr file or the order in the .lps file?
PascalDragon:
--- Quote from: jwdietrich on June 05, 2023, 09:47:36 pm ---According to FPC's documentation of units, "the initialization parts of the units are executed in the order that the compiler loaded the units when compiling a program". But what determines this order? Is it the order, in which the units are listed in the units section of the main program (or the .lpr file, if Lazarus is used)?
--- End quote ---
The units are essentially initialized in a depth first order. So if you have a program that uses UnitA and UnitB and UnitA uses UnitC then the order will be UnitA, UnitC and UnitB. Essentially don't rely on a specific order as it might change depending on what units you use. What you can rely on however is that if a unit's initialization section is run is that all units used in the interface section are initialized and - assuming there is no cycle - the ones in the implementation section as well.
--- Quote from: jwdietrich on June 05, 2023, 09:47:36 pm ---In the Lazarus IDE, the units are also listed in the XML-encoded .lps file. Is it also used for determining the order of the initialization section? And, if so, what takes precedence, the units section in the .lpr file or the order in the .lps file?
--- End quote ---
The Lazarus project files (except for the lpr) are irrelevant to what the compiler does.
jwdietrich:
Thanks, @PascalDragon.
Navigation
[0] Message Index