Recent

Author Topic: Include files in different folders  (Read 701 times)

Manlio

  • Full Member
  • ***
  • Posts: 164
  • Pascal dev
Include files in different folders
« on: February 29, 2024, 09:38:50 am »
The FPC documentation says the following about include files:

Quote
The compiler will look for the file to include in the following locations:
1. It will look in the path specified in the include file name.
2. It will look in the directory where the current source file is.
3. it will look in all directories specified in the include file search path.
https://lazarus-ccr.sourceforge.io/fpcdoc/prog/progsu40.html#x47-460001.2.40

However, whenever I try to follow point 1 above (specify a path in the include file name) I get an error. I tried both absolute and relative paths, for example:

{$include ..\inc_files\file.inc}
{$include C:\myfpc\inc_files\file.inc}

And so on. I'm sure the absolute and relative paths are correct and the file exists. I tried with \ and / path delimiters. I tried with and without quotes around the filenames. But the result is always an error: Fatal: Cannot open include file: "C:\myfpc\inc_files\file.inc"

The only way I found that works is to specify a folder for include files in the project options, and to use the file name without path in the $include directive. But I would prefer using relative paths once and for all, instead of having to specify Fi (include folders) in every single project I create.

In any case the official documentation clearly says that i should be able to specify a path with the filename, so why nothing seems to work?

Am I doing something wrong? Am I misreading the documentation? Can anyone kindly show me how to do it, or explain why it cannot be done?

Thank You!!
manlio mazzon gmail

rvk

  • Hero Member
  • *****
  • Posts: 6166
Re: Include files in different folders
« Reply #1 on: February 29, 2024, 09:50:11 am »
And so on. I'm sure the absolute and relative paths are correct and the file exists. I tried with \ and / path delimiters. I tried with and without quotes around the filenames. But the result is always an error: Fatal: Cannot open include file: "C:\myfpc\inc_files\file.inc"
Works fine for me.

Code: Pascal  [Select][+][-]
  1. {$include C:\Temp\Temp.txt}
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.  
  6. end;

Manlio

  • Full Member
  • ***
  • Posts: 164
  • Pascal dev
Re: Include files in different folders
« Reply #2 on: February 29, 2024, 10:07:42 am »
Works fine for me.
Code: Pascal  [Select][+][-]
  1. {$include C:\Temp\Temp.txt}
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.  
  6. end;

Then it must be me, I must be doing something wrong, I'll check everything again.

Thank you.
manlio mazzon gmail

TRon

  • Hero Member
  • *****
  • Posts: 2534
Re: Include files in different folders
« Reply #3 on: February 29, 2024, 10:54:35 am »
Then it must be me, I must be doing something wrong, I'll check everything again.
If all else fails... use the command-line option -va to produce an extensive log of the compilation process. It will show in detail which directories and files are tried by the compiler (amongst other things).

test:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$include ./include-files/yesterday/hitme.inc}
  4.  
  5. begin
  6.   writeln(in_the_face);
  7. end.
  8.  

include:
Code: Pascal  [Select][+][-]
  1. var
  2.   in_the_face: string = 'in the face';
  3.  

compilation:
Code: [Select]
fpc -B test.pas
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test.pas
Linking test
7 lines compiled, 0.1 sec

partial log from fpc -B -va test.pas
Code: [Select]
...
[0.012] Using object path: /home/apps/fpc/3.2.2/bin/x86_64-linux/
[0.012] Compiling test.pas
[0.012] Searching file test.pas... found
2 195/736 Kb Used
[0.012] test.pas(3,2)  Handling switch "$INCLUDE"
[0.012] Searching file ./include-files/yesterday/hitme.inc... found
[0.012] Searching file ./include-files/yesterday/hitme.inc... found
[0.012] test.pas(3,2)  Start reading includefile ./include-files/yesterday/hitme.inc
[0.012] (TEST)     Registering new unit SYSTEM
[0.012] (TEST)     Load from TEST (implementation) unit SYSTEM
[0.012] (SYSTEM)   Loading unit SYSTEM
[0.012] Unitsearch: system.ppu
[0.012] Searching file system.ppu... not found
...

What does not work (or at least is known not to work) is include file nesting (but the output log will then show you that it fails to include).
« Last Edit: February 29, 2024, 11:03:07 am by TRon »

paule32

  • Full Member
  • ***
  • Posts: 206
Re: Include files in different folders
« Reply #4 on: February 29, 2024, 12:09:14 pm »
I am use the Compiler Option:

-Fi<x>   // where <x> is the rel/abs Path to the "include" Files.

Then in the Source File(s):

{$i filename.inc}

This have the Advantage, that you don't need to Change the hard coded Source Files Path's, when the Directories are out of date or you modified it.

TRon

  • Hero Member
  • *****
  • Posts: 2534
Re: Include files in different folders
« Reply #5 on: February 29, 2024, 12:38:57 pm »
I am use the Compiler Option:

-Fi<x>   // where <x> is the rel/abs Path to the "include" Files.
Do note that in case that you use a relative path in that case, that it could trigger including the wrong file because well... the path is relative   :)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11454
  • FPC developer.
Re: Include files in different folders
« Reply #6 on: February 29, 2024, 02:13:13 pm »
Try if you get differing behaviour when you rename "file.inc" to something else (file1.inc)

Specially if you don't use a release version (RTL etc compiled with -Ur).

This because there is a file.inc in the RTL, and it might trigger the compiler to recompile the rtl because the inc file was changed. (you can see if this happens in the -va log, as TRon said)

(and I'm not a fan of any paths in sources, absolute or not)
« Last Edit: February 29, 2024, 02:54:51 pm by marcov »

paule32

  • Full Member
  • ***
  • Posts: 206
Re: Include files in different folders
« Reply #7 on: February 29, 2024, 02:50:21 pm »
Yes, I use relative Path, too.

I am not a Friend of absolute Path, because I had some Hard Drive Storage Damages in the Yesterday Years - and then I had no Backup, and different Location's of the Sources.

I work with Windows 10, and the Security Settings can be scary.
Because all Security Level's will be lost on new Setup's.

 

TinyPortal © 2005-2018