Recent

Author Topic: Question about $include  (Read 889 times)

ydd

  • New Member
  • *
  • Posts: 39
Question about $include
« on: January 17, 2021, 04:16:25 am »
documentation: https://wiki.freepascal.org/$include

I have unit "SomeVeryLongUnitName" and I want to include: "SomeVeryLongUnitName_RT"

so I need to use: {$include SomeVeryLongUnitName_RT}

but there is special tag %file%:
  {$include %file%} expands to the file’s name the directive is found in

How I can use something like:
  {$include %file%_RT}

?

MarkMLl

  • Hero Member
  • *****
  • Posts: 2330
Re: Question about $include
« Reply #1 on: January 17, 2021, 10:37:48 am »
Curious: I don't see mention of the include directive in the official documentation at https://www.freepascal.org/docs-html/current/prog/prog.html It does however document $includepath, but I'm not sure that will help

I suspect that it's not possible to do what you want without using an external program as a preprocessor, one of the problems is that  $I %file%  expands as a quoted string so even if the expansion worked it wouldn't be quite as expected.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lucamar

  • Hero Member
  • *****
  • Posts: 3741
Re: Question about $include
« Reply #2 on: January 17, 2021, 12:30:42 pm »
Curious: I don't see mention of the include directive in the official documentation at https://www.freepascal.org/docs-html/current/prog/prog.html

Huh? It's there all right ... (see attached image)

As for the OP's question, no it can not be used that way; {$I %somevar%} inserts a string constant in the source, which is not what you want.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2330
Re: Question about $include
« Reply #3 on: January 17, 2021, 01:05:34 pm »
Curious: I don't see mention of the include directive in the official documentation at https://www.freepascal.org/docs-html/current/prog/prog.html

Huh? It's there all right ... (see attached image)

As for the OP's question, no it can not be used that way; {$I %somevar%} inserts a string constant in the source, which is not what you want.

Well, I did say it was curious... I'd been peering at that page and I'm sure I tried a search and for some reason couldn't see it (and yes, I do know there's two lists on the page). If I had been able to see it I might have been a bit less guarded in pointing out the distinction between definitive documentation and apocrypha in the wiki, which is something I had to come to terms with many years ago when dealing with both Microsoft and Microsoft Press and discussing things like the Petzold books (published by the latter, hence not acknowledged as documentation by the former).

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 2863
  • Compiler Developer
Re: Question about $include
« Reply #4 on: January 17, 2021, 05:10:24 pm »
How I can use something like:
  {$include %file%_RT}

You can't. Simple as that.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2330
Re: Question about $include
« Reply #5 on: January 17, 2021, 05:40:25 pm »
You can't. Simple as that.

Can you see any way round it?

For example, can . be knocked out of the include path so that the include file could be in a subdirectory?

OP, tell us what you're trying to do and why the name can't be hardcoded.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kays

  • Sr. Member
  • ****
  • Posts: 299
  • Whasup!?
    • KaiBurghardt.de
Re: Question about $include
« Reply #6 on: January 17, 2021, 05:57:23 pm »
[…] Can you see any way round it?
You can do something else. You can utilize the {$include *} directive. If you check out compiler/scanner.pas, you see only the file suffix (usually separated by a dot) is preserved. So (if you can change the file name patterns) how about writing something like:
Code: [Select]
$ head foo.pas foo.RT
==> foo.pas <==
program foo(output);
begin
        writeLn({$include *.RT});
end.

==> foo.RT <==
'Abracadabra'

If you aren’t allowed to change the file name, you may utilize a (symbolic) link foo.RTfoo_RT.
Yours Sincerely
Kai Burghardt

MarkMLl

  • Hero Member
  • *****
  • Posts: 2330
Re: Question about $include
« Reply #7 on: January 17, 2021, 07:08:14 pm »
You can do something else. You can utilize the {$include *} directive.

That has some interesting possibilities, but I can see it interacting badly with the possible unit name issues I raised a few days ago https://forum.lazarus.freepascal.org/index.php/topic,52884.msg390815.html

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ydd

  • New Member
  • *
  • Posts: 39
Re: Question about $include
« Reply #8 on: January 18, 2021, 12:24:17 am »
You can do something else. You can utilize the {$include *} directive

it works for me, I can use: {$I *.RT} - for file with name: SomeVeryLongUnitName.RT

but I cannot use like this: {$I *.RT.pas} - for file with name: SomeVeryLongUnitName.RT.pas

error - Fatal: Syntax error, "BEGIN" expected but "UNIT" found

Kays

  • Sr. Member
  • ****
  • Posts: 299
  • Whasup!?
    • KaiBurghardt.de
Re: Question about $include
« Reply #9 on: January 18, 2021, 12:45:26 am »
[…] I cannot use like this: {$I *.RT.pas} […]
No, this won’t work, this notation only allows you to specify a single possibly non-standard (file type hint) suffix. It’s not replace and concatenate. You would’ve seen that if you took a look at source code lines I referenced above.
Earlier today I’ve expanded the wiki page on that pointing out just that.

You still can create a symbolic link foo.RTfoo.RT.pas, but it’s a cumbersome solution relying on file system capabilities. Also, AFAIK SCMs like the popular git(1) won’t adequately manage them. (Or was it just hard links that won’t be preserved? Anyway, it’s inconvenient.)
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018