"b.h" is being processed directly by curren" /> "b.h" is being processed directly by curren" /> "b.h" is being processed directly by curren"/>

Is there any way to check whether header file included directly or indirectly in single compilation unit for gcc?

71 views Asked by At
// a.cc  
#include "a.h"
// a.h  
#include "b.h"  
#include "c.h"
// b.h  
#include "c.h"
// c.h
#include <string>

"b.h" is being processed directly by current compliation unit
"c.h" is being processed indirectly by "gcc -H"

I want to prevents developers from including "c.h" directly in "a.cc".A way to specify some metadata rules by cmake/gcc, like bazel

gcc has plugin event "PLUGIN_INCLUDE_FILE", Unfortunately, the information provided, unlike clang, is not sufficient to do this, (maybe I didn't find it)

P.S. I want to do something like bazel layercheck(https://bazel.build/reference/be/c-cpp#hdrs )(clang -fmodules-decluse blabla...), using cmake and gcc toolchain
P.S. "xx.h" and "xx.cc" is component unit if the same base name

1

There are 1 answers

4
Maxim Egorushkin On

Command line option -H causes gcc print the name of each header file included.

Automatically generated .d header dependency files also contain full paths of all files included for a .o file.


i want to prevents developers from including "c.h" directly in "a.cc"

You can use the same method gcc uses for this purpose. E.g. /usr/lib/gcc/x86_64-linux-gnu/11/include/avx2intrin.h:

#ifndef _IMMINTRIN_H_INCLUDED
# error "Never use <avx2intrin.h> directly; include <immintrin.h> instead."
#endif