3

Is there a way to tell gcc/g++/clang where to look for headers that are included via angle brackets ("<", ">")?

I don't use the angle bracket convention for non-system files, but the problem is that when I try using the headers from some packages I download, I get errors for all of the included files.

For example, say I want to include headers from a module called Foo that I download:

/foo-v1.0/include/DependencyA.hpp:

#ifndef DEP_A_HPP
#define DEP_A_HPP

class DependencyA
{
  ...
};

#endif

/foo-v1.0/include/Api.hpp:

#ifndef FOO_HPP
#define FOO_HPP
#include <Foo/DependencyA.hpp>

void doSomething(DependencyA* da);

#endif

Then, in my own code:

/mycode.cpp:

#include "/foo-v1.0/include/Api.hpp"

DependencyA* da = new DependencyA();
doSomething(da);

I get a compile error: fatal error: 'Foo/DependencyA.hpp' file not found

I've tried building with:

  • clang -c mycode.cpp -isystem./foo-v1.0/include -o mycode.o
  • clang -c mycode.cpp -isystem./foo-v1.0/include/ -o mycode.o
  • clang -c mycode.cpp -I./foo-v1.0/include -o mycode.o
  • clang -c mycode.cpp -I./foo-v1.0/include/ -o mycode.o

and so on, to no avail.

How do I tell the compiler to resolve <Foo/**/*> to a particular root directory for every included file?

fuszz2
  • 31
  • 2
  • 2
    You've got a problem. None of the actual file paths terminate in `Foo/DependencyA.hpp` Setting the search path can't overcome that, the compiler is trying to add `Foo/DependencyA.hpp` to each directory you list in the include search path. – Ben Voigt Jan 13 '21 at 22:37
  • 1
    You might want to create a `Foo` symlink (perhaps in `/usr/local/include`, perhaps in some project-local directory you add via `-isystem`) pointing to `/foo-v1.0/include` – Ben Voigt Jan 13 '21 at 22:38
  • If you tyoe `man gcc` you will read the documentation for the `gcc` command, which includes the setting/option you're looking for. It is true that, for the reasons stated above, the appropriate `gcc` setting/option won't help you anyway; however you should be aware that `man gcc` provides complete documentation for all available gcc options. Knowing where to find and how to read technical documentation is a required skill for every C++ developer. – Sam Varshavchik Jan 13 '21 at 23:07

1 Answers1

1

The answer is already in the comments.

To check include dirs one can use the method described here: What are the GCC default include directories? , preferably with - replaced with /dev/null:

clang -xc -E  -v /dev/null

On my machine for clang it gives

ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/clang/11.0.0/include
 /usr/include
End of search list.

To discover how to manipulate this list, it suffices to read the gcc (or clang) manual (man clang or find it in the Internet, for example, https://man7.org/linux/man-pages/man1/gcc.1.html ). For gcc this reads:

Options for Directory Search
       These options specify directories to search for header files, for
       libraries and for parts of the compiler:

       -I dir
       -iquote dir
       -isystem dir
       -idirafter dir
           Add the directory dir to the list of directories to be searched
           for header files during preprocessing.  If dir begins with = or
           $SYSROOT, then the = or $SYSROOT is replaced by the sysroot
           prefix; see --sysroot and -isysroot.

           Directories specified with -iquote apply only to the quote form
           of the directive, "#include "file"".  Directories specified with
           -I, -isystem, or -idirafter apply to lookup for both the
           "#include "file"" and "#include <file>" directives.

This description is followed by a detailed description of the order in which header files are searched and by some recommendations as to which option to use for which purpose. You'll find it in the manual. Search for "Options for Directory Search".

What I really don't like in your code is this line:

#include "/foo-v1.0/include/Api.hpp"

It seems to contain the absolute path to the header and I've never seen anything like this. I would change it to

#include "Api.hpp"   

with /foo-v1.0/include being added to the search list via the usual compiler -I command-line option.

zkoza
  • 2,644
  • 3
  • 16
  • 24