Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C preprocessor include and paths

Say we have a .cpp file A including a .h file B which includes a .h file C.

// File A.cpp
#include "B.h"
...

// File B.h
#include "../../utilityies/C.h"
...

If B includes C using a relative path, is that relative path resolved relatively to the location of B or to the location of A which is the .cpp that B is going to be copy-pasted into ?

like image 915
Virus721 Avatar asked Aug 30 '26 10:08

Virus721


1 Answers

The include path is relative to the file containing it so in your case relative to the location of B.

From the gcc docs:

GCC looks for headers requested with #include "file" first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat.h contains #include "types.h", GCC looks for types.h first in /usr/include/sys, then in its usual search path.

like image 87
fun4jimmy Avatar answered Sep 02 '26 05:09

fun4jimmy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!