How to include C++ header file which is not in the same directory as the current source file without using '..'?

For example:

project/utility/a.cpp

and

project/utility/a.h

are implementation file and header file for a batch of functions that are used everywhere in this project.

Now, imagine

project/component/b.h

would like to include a.h. A common solution is to use:

#include "../utility/a.h"

in b.h.

But this is not allowed in Google C++ style:

All of a project's header files should be listed as descendants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory).

I tried to include it as showed in the document above, but it does not work.

So what should I do to include it without using '.' or '..' ?

See also What are the benefits of a relative path such as "../include/header.h" for a header? and Should I use #include in headers?

You need to decide on whether to use just the base name of the header file (a.h in your question's abbreviated example), or whether to use a subdirectory plus header name project/a.h). Either is workable as long as the header names are unique; the subdirectory version is often better and requires fewer command line options, in general.

You can write:

#ifndef B_H_INCLUDED
#define B_H_INCLUDED

#include "utility/a.h"

…other material required…

#endif /* B_H_INCLUDED */

and then, on the compiler command line, include an option:

-I ..

This ensures that the compiler will find utility/a.h as ../utility/a.h (unless there's a name conflict somewhere). Or you can specify the absolute path to your project:

-I /path/to/project

which will read /path/to/project/utility/a.h.

C/C++ #include directive with Examples, #include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. #include directive (C/C++) 08/29/2019; 4 minutes to read +1; In this article. Tells the preprocessor to treat the contents of a specified file as if they appear in the source program at the point where the directive appears.

So what should I do to include it without using '.' or '..' ?

You specify a path to the top include directory using the -I option of the compiler.

C - Header Files, There are two types of header files: the files that the programmer writes and the files that comes with your compiler. You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio. h header file, which comes along with your compiler. In the C Programming Language, the #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #include directive is found. Include directives are typically used to include the C header files for C functions that are held outsite of the current source file.

Normally, you should use Makefile or better to use automake. Since you are using C++, your make file simply add the header path to the CXXFLAGS variable.

----- your make file ----- Makefile:

CXXFLAGS += -I../utility
VPATH = ../utility

yourprog : b.o 
    $(CXX) $(CXXFLAGS) -o $@ $^ ../utility/a.o

In your source file, you only need to

#include "a.h"

In the Makefile, it is better to also add LDFLAGS. This segment of the Makefile is only showing the relevant part. A complete Makefile has many more lines of code. the -I compiler option tells the compiler to find herder files in the directory. You can have multiple -I options for the compiler. This is similar to the PATH variable in unix where your search path is separated by ":"

Include Syntax (The C Preprocessor), Both user and system header files are included using the preprocessing directive ' #include '. It has two variants: #include < file >. This variant is used for system� If you have an individual C function that you want to call, and for some reason you don’t have or don’t want to #include a C header file in which that function is declared, you can declare the individual C function in your C++ code using the extern "C" syntax. Naturally you need to use the full function prototype:

How to mix C and C++, C++ FAQ, In the C Programming Language, the #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #� Empty C headers. The headers <complex.h>, <ccomplex>, <tgmath.h>, and <ctgmath> do not contain any content from the C standard library and instead merely include other headers from the C++ standard library. The use of all these headers is deprecated in C++.

Why do we use #include in C?, foo.h #ifndef FOO_H_ /* Include guard */ #define FOO_H_ int foo(int x); /* An example function declaration */ #endif // FOO_H_. foo.c #include� ** 3) Include guards ** ***** C++ compilers do not have brains of their own, and so they will do exactly what you tell them to. If you tell them to include the same file more than once, then that is exactly what they will do. And if you don't handle it properly, you'll get some crazy errors as a result:

Creating your own header file in C, C/C++[edit]. In C and C++, the #include preprocessor directive causes the compiler to replace that line with the entire text of the� Thus, #include <x/*y> specifies inclusion of a system header file named x/*y. However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus, #include "x \\y" specifies a filename containing three

Comments
  • You specify a path to the top include directory using the -I option of the compiler.
  • Thanks a lot ! This method is awesome..