提取包含在 C 文件中

Shm*_*ser 1 grep c perl

我需要在 C 文件中提取所有包含的库,但我遇到了一些问题。我对此的解决方案是这样的:

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

例如,此正则表达式在注释中采用库

/*#include <stdlib.h>*/
Run Code Online (Sandbox Code Playgroud)

而且我不知道如何让该脚本只给我没有#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() {

}
Run Code Online (Sandbox Code Playgroud)

我想要的是:

"string.h"
<stdlib.h>
"math.h"
"stdint.h"
<fenv.h>
"something.h"
"float.h"
Run Code Online (Sandbox Code Playgroud)

Ste*_*itt 6

您应该要求编译器(或者更确切地说,是 C 预处理器)为您完成这项工作:

gcc -M main.c
Run Code Online (Sandbox Code Playgroud)

这将产生一个 Makefile 样式的依赖项,main.c其中包含的所有标头(包括那些传递包含的标头)。它将正确处理注释和其他预处理器指令(#if#ifdef等等)。