Positioning of $LINK pragma lazarus / 21-General ---------------------------------------------------------------------- MarkMLl · 2025-01-30 20:13 What are the rules that relate to the precise positioning of a $LINK in a unit? Does it have to appear before the uses clause, after the uses but before consts, after consts but before functions, or just /somewhere/? Context: https://forum.lazarus.freepascal.org/index.php/topic,69942.0.html MarkMLl ---------------------------------------------------------------------- PascalDragon · 2025-01-30 20:40 Quote from: MarkMLl on January 30, 2025, 09:13:21 pmor just /somewhere/? Somewhere is good ---------------------------------------------------------------------- MarkMLl · 2025-01-31 08:08 Thanks very much. The most natural place (from the POV of the program generating wrappers for the .inc file) is with the procedure declarations, but the best place (from the POV of legible code) is probably close to the uses clause. MarkMLl ---------------------------------------------------------------------- n7800 · 2025-01-31 18:43 You can use the documentation to find out. There are local and global directives. As you can see, the local directive {$LINK} can be specified anywhere in the program. ---------------------------------------------------------------------- MarkMLl · 2025-01-31 20:27 I checked the documentation before posting, and it doesn't say anything specific. MarkMLl ---------------------------------------------------------------------- n7800 · 2025-01-31 21:24 The phrase of local directives (i.e. {$LINK}): QuoteThe local directives influence the compiler’s behavior from the moment they’re encountered until the moment another switch annihilates their behavior, or the end of the current unit or program is reached.indicates that they can appear anywhere in the program. Whereas for global directives it is explicitly stated: QuoteThey must be specified before the first statement different from the program, unit or library clause in a source file, or they will have no effect. ---------------------------------------------------------------------- Halina00 · 2025-02-01 00:46 Quote from: MarkMLl on January 30, 2025, 09:13:21 pmWhat are the rules that relate to the precise positioning of a $LINK in a unit? Does it have to appear before the uses clause, after the uses but before consts, after consts but before functions, or just /somewhere/? Context: https://forum.lazarus.freepascal.org/index.php/topic,69942.0.html MarkMLl In Free Pascal, the `$LINK` directive doesn’t have super strict placement rules, but there are some best practices. It typically goes after the `interface` or `implementation` section starts, but before any functions or procedures that rely on the linked object. You don’t have to place it before or after `uses` or `const` specifically, but it’s common to put it right after the `uses` clause for clarity. Just avoid placing it in the `interface` section before `uses`, as that might cause issues. So, in short: after `uses` is safe, but before you start calling linked functions. ---------------------------------------------------------------------- MarkMLl · 2025-02-01 07:40 Quote from: Halina00 on February 01, 2025, 01:46:01 amSo, in short: after `uses` is safe, but before you start calling linked functions. Sven- a core developer, said "somewhere", but I agree with your comments as an aid to clarity. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2025-02-01 07:42 Quote from: n7800 on January 31, 2025, 10:24:16 pmThe phrase of local directives (i.e. {$LINK}): QuoteThe local directives influence the compiler’s behavior from the moment they’re encountered until the moment another switch annihilates their behavior, or the end of the current unit or program is reached.indicates that they can appear anywhere in the program. Whereas for global directives it is explicitly stated: QuoteThey must be specified before the first statement different from the program, unit or library clause in a source file, or they will have no effect. The fact that once given a $link can't be revoked, and that Sven says that "somewhere" is OK, suggests that (your interpretation of) the documentation is wrong. MarkMLl