3

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 '..' ?

1
  • 5
    You specify a path to the top include directory using the -I option of the compiler. Commented May 17, 2015 at 17:36

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

1

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 ":"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.