开发者

find whether a string is present directly or indirectly in all files

开发者 https://www.devze.com 2023-04-11 00:40 出处:网络
I have set of c files and I need to find out whether a particular header file is directly or indirectly present in all the files.

I have set of c files and I need to find out whether a particular header file is directly or indirectly present in all the files.

For ex:-
Header files:-

hf1.h:-
int i = 10;

hf2.h:-
#include "hf1.h"

hf3.h:-
#include <hf1.h>

hf4.h:-
#include <hf3.h>

c files:-
cf1.c:-
#include <hf1.h>

cf2.c:-
#include <hf2.h>

cf3.c:-
#include <hf4.h>

In 开发者_Go百科this case hf4.h indirectly included hf1.h file. I need to find our whether hf1.h is directly or indircetly included in all the files. Let me know if anyone need more clarification.

Any unix commands or shell scripts to solve this problem.


  1. Introduce a #error directive into hf1.h
  2. make clean
  3. make -k all
  4. All object files that now exist have not included hf1.h

The -k step is the crucial one: It ensures that compilation continues past the first broken file.


A long way would be to use the -E option in gcc (with this parameter it actually gives the output after pre-processing only, no compilation is done) for all the c files and then search for parts of each header files in all the outputs. Will update if any shorter method comes to mind.


Assuming you do not have cases of code like

#include               <hf1.h>
#include \
<hf1.h>

then finding the direct includes is a simple

grep -l '#include .hf1\.h'

command. For finding the indirect includes I suggest that you put

#error "hf1_included"

into hf1.h and compile all files (e.g. "make -k" or similar). Capture stderr and search for hf1_included. This will give a list of files that either directly or indirectly includes hf1.h. Now you can extract a list of only indirect includes by also taking into account the first list of only direct includes. You can use the program comm to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号