【问题标题】:Print out function name in a c file using shell script?使用shell脚本打印出c文件中的函数名?
【发布时间】:2018-09-09 05:38:25
【问题描述】:

例如,如果我有 C 文件:

game.c
int main(){
    //code
    }

hello.c
int something(){
    //code
}

我将如何获得输出:

/directory/path/game.c
int main()

/directory/path/hello.c
int something()

我已经编写了一个 for 循环来遍历目录中以 .c 结尾的文件,但不确定如何输出函数名?

所以我试图执行一个 shell 脚本,它会读取 C 文件,然后输出它的函数名。

for file in $(find $1 -type f -name '*.c')
do
    echo $file
    grep -r "what to search for??"

done

【问题讨论】:

  • 上下文不清楚:您是在执行 C 程序并想要输出函数,还是在执行应该 读取的 shell 脚本(哪个 shell?) > 未编译的 C 源文件并打印在那里找到的函数?
  • 您是否尝试使用 shell 脚本列出各种 .c 文件中定义的函数?
  • 如果你在 gcc 下,aux-info 在循环中可以提供帮助:stackoverflow.com/q/17163583/1606345
  • 对不起,我应该更清楚。我正在执行一个 shell 脚本,它应该读取所​​有 C 文件,然后打印出函数名称

标签: c shell


【解决方案1】:

您可以使用正则表达式从文件中提取函数声明。你可以看这里:Regex to pull out C function prototype declarations?

如果该行与正则表达式匹配,则可以打印它。

【讨论】:

  • 我认为你错过了问题的重点。 shell 脚本应该列出源文件中定义的函数。
  • 实际上我认为由于用户在问题的两个函数中插入了//code,因此他要求在函数本身中输入一些代码。谢谢指点
猜你喜欢
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2022-11-10
相关资源
最近更新 更多