从 BASH 获取 C 函数的头文件

Mat*_*ock 6 c header-file

我想获得给定 C 函数所需的标头 - 例如,当我想查看要使用的内容时fork(),我可以输入man fork并查看它是#include <unistd.h>. 但是,我想“自动”执行此操作(无需我看屏幕),因此我可以将输出重定向到文件以自动插入标题。我怎样才能做到这一点?

Jam*_*den 1

除非您想编写一个联机帮助页解析器,否则我认为最好的选择是构建一个文本文件并手动修复它。从这样的事情开始:

$ ls /usr/share/man/man2 | sed 's/\.2.*//' \
  | while read F
    do 
      I=$(man $F | grep '#include'| head -1 | sed 's/>.*$/>/')
      printf '%15s:\t%s\n' $F "$I"
    done
Run Code Online (Sandbox Code Playgroud)

我得到的前 10 行显示成功率为 80%;有两页没有提到包含文件:

         accept:           #include <sys/types.h>
        accept4:           #include <sys/types.h>
         access:           #include <unistd.h>
           acct:           #include <unistd.h>
        add_key:           #include <keyutils.h>
       adjtimex:           #include <sys/timex.h>
    afs_syscall:
          alarm:           #include <unistd.h>
alloc_hugepages:
     arch_prctl:           #include <asm/prctl.h>
Run Code Online (Sandbox Code Playgroud)

然后,为了变得非常懒,使用一个小函数来搜索你的文件,比如

$ inc() { awk -F\\t -e "/^ *$1/ "'{print $2}' /tmp/inc; }
$ inc fork
   #include <unistd.h>
Run Code Online (Sandbox Code Playgroud)

请注意,某些手册页提到了多个头文件,具体取决于使用该页面上的功能或函数。有关示例,请参见套接字(2)。