C中如何获取一个文件中所有函数的范围(即行号)?

发布于 2024-12-14 19:56:41 字数 62 浏览 5 评论 0原文

我想在C语言中获取一个文件中所有函数的开始和结束行号。有谁知道Linux中是否有一个易于使用的工具用于此目的?

I want to get both the beginning and ending line numbers of all functions in a file in C. Does any one know that whether there is a easy-to-use tool in Linux for this purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街九尾狐 2024-12-21 19:56:42
$ ctags -x --c-kinds=f filename.c

这仅给出了每个函数的起始行,但也许这已经足够好了。

如果代码是使用相当常见的约定编写的,则该函数应以第一列中包含 } 的单行结尾,因此在给定第一行的情况下很容易获得最后一行:

awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c
$ ctags -x --c-kinds=f filename.c

This only gives the starting line of each function, but perhaps that is good enough.

If the code was written using fairly common conventions, the function should end with a single line containing } in the first column, so it is fairly easy to get the last line given the first:

awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文