Cファイルに含まれるコンテンツの抽出

Cファイルに含まれるコンテンツの抽出

含まれているすべてのライブラリをCファイルに抽出する必要がありますが、いくつかの問題があります。私の解決策は次のとおりです。

grep -oP '#(?:\/\*\w*\*\/|)include(?:\/\*\w*\*\/|\s*)"\w*.h"|<\w*.h>' main.c

たとえば、この正規表現はライブラリを使用します。

/*#include <stdlib.h>*/

このスクリプトを作成する方法がわかりません。 #include なしでライブラリ名を指定するだけです。

正規表現が正しく機能するように修正するにはどうすればよいですか?

修正する:

メインプログラム

#include  "string.h"
#include <stdlib.h>
 #include "math.h"
    #include "stdint.h"
#/*comment*/include /*comment*/ <fenv.h>  
//#include <iostream>
#/**/include "something.h"
/* comment */#include "float.h"
/*#include "library.h"*/
int main() {

}

私が望むもの:

"string.h"
<stdlib.h>
"math.h"
"stdint.h"
<fenv.h>
"something.h"
"float.h"

ベストアンサー1

コンパイラ(またはCプリプロセッサ)にこれを行うように依頼する必要があります。

gcc -M main.c

main.cこれにより、埋め込まれたすべてのヘッダー(遷移的に埋め込まれたヘッダーを含む)を含むMakefileスタイルの依存関係が生成されます。コメントやその他のプリプロセッサディレクティブ(#ifなど)を正しく処理します#ifdef

おすすめ記事