0

I created a very simple hello world program in C and I'm trying to call a function from an included file. The problem is the gcc program is strange in how it recognizes files.

Suppose the main file is named a.c and the file I want to include is b.c

So in my headers, I add:

#include <./b.c>

and when I run gcc 5.3.0, I receive this error:

./a.c:xx:yy: fatal error: ./b.c: No such file or directory
compilation terminated.

(where xx is the line number of the include line and yy is where the > is located on that line.)

I also tried another idea. I executed gcc while including the library path as follows:

 gcc -I./ a.c

and I still get the same error.

In all tests, the file b.c exists in the same folder as a.c is and that I execute gcc from.

Now If I change the include line in the source to the following:

#include </path/to/b.c>

(thereby replacing the relative path with an absolute path) and run gcc, the file is then read for processing.

Is there a way I can change my code so I don't have to constantly specify absolute paths if I need to include multiple custom C files that are all in the same folder as the code that references them?

Like... is there such thing as....

#setlibpath /path/to/my/c/files
#include <./item.c>
#include <./item2.c>
....
#include <./itemn.c>

or do I have to do this....

#include </path/to/my/c/files/item1.c>
#include </path/to/my/c/files/item2.c>
....
#include </path/to/my/c/files/itemn.c>
Mike
  • 9
  • 2
  • 2
    You don't include C files. Build them separately and link them. – stark Jan 02 '22 at 03:22
  • 2
    Use double quotes around the name and the header (source file) will be found. Use angle brackets around system headers. And generally, avoid using `./` (and [`../` even more so](https://stackoverflow.com/q/597318/15168)) in header names. For GCC, see also [Include Operation](https://gcc.gnu.org/onlinedocs/cpp/Include-Operation.html#Include-Operation) and the following sections. – Jonathan Leffler Jan 02 '22 at 03:46
  • 1
    Also see POSIX [`c99`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html) for a the `-I` option: _Change the algorithm for searching for headers whose names are not absolute pathnames to look in the directory named by the directory pathname before looking in the usual places. Thus, headers whose names are enclosed in double-quotes (`""`) shall be searched for first in the directory of the file with the #include line, then in directories named in `-I` options, and last in the usual places._ […continued…] – Jonathan Leffler Jan 02 '22 at 03:55
  • […continuation…] _For headers whose names are enclosed in angle brackets (`<>` ), the header shall be searched for only in directories named in `-I` options and then in the usual places. Directories named in `-I` options shall be searched in the order specified. If the `-I` option is used to specify a directory that is one of the usual places searched by default, the results are unspecified._ Note that `./b.c` is not an absolute pathname — so the name is appended to a set of directories, and the current directory is not used unless the `#include` line uses double quotes. – Jonathan Leffler Jan 02 '22 at 03:57

1 Answers1

1

Converting my comments into an answer.

Use double quotes around the name and the header (source file) will be found. Use angle brackets around system headers. And generally, avoid using ./ (and ../ even more so) in header names. For GCC, see also Include Operation and the following sections.

Also see the POSIX specification for the c99 compiler for the -I option:

Change the algorithm for searching for headers whose names are not absolute pathnames to look in the directory named by the directory pathname before looking in the usual places. Thus, headers whose names are enclosed in double-quotes ("") shall be searched for first in the directory of the file with the #include line, then in directories named in -I options, and last in the usual places. For headers whose names are enclosed in angle brackets (<>), the header shall be searched for only in directories named in -I options and then in the usual places. Directories named in -I options shall be searched in the order specified. If the -I option is used to specify a directory that is one of the usual places searched by default, the results are unspecified.

Note that ./b.c is not an absolute pathname — so the name is appended to a set of directories, and the current directory is not used unless the #include line uses double quotes.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278