#include <> Vs #include ""
Kexin Tang

#include <header>

This will let preprocessor find header files in pre-designated directories.

These directories are normally system-related, such as “/usr/include”, “/x86/include”, etc.

Let’s use Linux as example. If we ls /usr, you will see bin, include and lib, and if we ls /usr/include, output will contain lots of system defined header files, such as /usr/include/cpp/vector and /usr/include/cpp/iostream. This is how we include these commonly used packages.

1
2
// find header file named "iostream" from "/usr/include"
#include <iostream>

#include "header"

This will search programmer-defined header files and typically includes same directory as the file containing the directive.

1
2
3
4
my_project
| - include
| | - my_header.h
| - my_project.cpp
1
2
// my_project.cpp
#include "my_header.h"

Let’s assume the project tree looks like that, and when we #include "", it will search header files under /my_project directory.

If the processor cannot find header file in current project directory, it will search header files in system path (the same as #include <>).

#include "header.h" – cannot find locally –> #include <header.h>


Add include path

As we discussed before, #include <> will search pre-designated directories. What if we want the processor to search other directories?

We can add include paths via

  • -I
  • system environment variable
  • CMake

After adding include paths, the searching order is

  • #include <>
    • dir list
    • system dir
  • #include ""
    • local
    • dir list
    • system dir

-I

We can use -I flag in command line to add search paths.

1
2
g++ -c test.cpp -o test
g++ -c test.cpp -o test -I /path/to/certain/include/

System environment variable

The system environment has a property called CPLUS_INCLUDE_PATH, we can set this to add search paths.

1
export CPLUS_INCLUDE_PATH="path/to/certain/include"

CMake

For CMake, we can use include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...]) .