# Include line problem

**URL:** <https://fortran-lang.discourse.group/t/include-line-problem/6261>\
**Category:** Help\
**Created:** [August 2, 2023, 12:00am UTC](https://fortran-lang.discourse.group/t/include-line-problem/6261 "2023-08-02T00:00:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [August 2, 2023, 12:00am UTC](https://fortran-lang.discourse.group/t/include-line-problem/6261/1 "2023-08-02T00:00:13Z")

</div>

Wishing to include a file `~/barfoo.f90` in a program `~/Jfh/foobar.f90` I wrote this to test how include lines work in my Linux system:

```auto
program foobar
  include '~/barfoo.f90'
end program

```

Unfortunately neither gfortran nor ifort could open the included file. But if I changed `~` to `..` in the include line both compilers compiled and ran it. I would have preferred `~` because I might sometimes want to use include lines in programs deeper in the directory tree. Can it be done?

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [August 2, 2023, 1:20am UTC](https://fortran-lang.discourse.group/t/include-line-problem/6261/2 "2023-08-02T01:20:03Z")

</div>

Both compilers allow the INCLUDE search path to be specified on the command line.

For gfortran see [Directory Options (The GNU Fortran Compiler)](https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html)

For Intel Fortran [https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-1/use-include-files.html](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-1/use-include-files.html)

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [August 2, 2023, 2:20am UTC](https://fortran-lang.discourse.group/t/include-line-problem/6261/4 "2023-08-02T02:20:41Z")

</div>

Thank you @DavidB and @kargl - problem solved. The happiest way would have been for an `include file_name` line to allow a non-constant `file_name` but I suspect that’s not coming in F202Y.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [August 2, 2023, 5:39am UTC](https://fortran-lang.discourse.group/t/include-line-problem/6261/5 "2023-08-02T05:39:51Z")

</div>

> [@Harper](#):
>
> But if I changed `~` to `..` in the include line both compilers compiled and ran it.

The reason for the difference is that `~` is expanded by a shell (usually to the same thing as the $HOME environment variable), while `..` and `.` are part of the file system. If you do an `ls`, then you don’t see them because they are invisible (they begin with periods), but if you do `ls -a` to show invisible files, then you do see them. Every directory has those files defined automatically by the file system, you do not need to define them yourself (e.g. as hard or soft links).

```auto
$ mkdir xxx
$ ls -a xxx
. ..

```

So in fortran, for example in an open statement, if you want to access files relative to your home directory, you would typically do something like the following:

```auto
PROGRAM test_getenv
  CHARACTER(len=255) :: homedir
  CALL get_environment_variable("HOME", homedir)
  WRITE (*,*) TRIM(homedir)
END PROGRAM

```

which is the example in the gfortran documentation. You would then reference your files relative to your home directory as, for example,

```auto
TRIM(homedir) // '/myfile'
TRIM(homedir) // '/mydir/myfile'

```

As you have determined, this does not work with fortran `INCLUDE`, which requires a literal constant, not a fortran variable or a parameter.

There are a few alternatives for the `INCLUDE` case. Some compilers allow the search path for `INCLUDE` to be specified through an environment variable or through a command line option. There is also the de facto standard preprocessor `#include` and macro substitution that can be used in a variety of ways.
