如何从 C/C++ 中提取特定函数 源代码文件,供后续处理

发布于 2024-07-13 22:37:24 字数 219 浏览 5 评论 0原文

我正在寻找一种简单的方法来从某些 C/C++ 源代码中打印出特定函数。 例如,假设 test.c 中定义了多个函数。 我希望能够打印出仅与这些函数之一相关的源代码。

编辑:抱歉,我应该更清楚我的最终目标。 我希望将该函数打印到屏幕上,以便我可以使用 wc 来获取该特定函数的字数。 另外,我希望它成为命令行工具链的一部分,因此它不是手动输入文件和选择文本的选项。

I am looking for an easy way to print out a specific function from within some C/C++ source code. For example, assume that test.c has several functions defined within it. I want to be able to print out the source code associated with only one of those functions.

Edit: Sorry, I should be a bit more clear about my end goal. I want the function printed to the screen so I can use wc to grab the word count of this specific function. Also, I want this be part of a command line tool-chain so it isn't an option to manually enter files and select the text.

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

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

发布评论

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

评论(3

栖竹 2024-07-20 22:37:24

您可以通过 doxygen 运行您的项目。 它将索引您的所有函数(以及类、结构等),并可以使它们以多种格式< /a> (包括 PDF 和 HTML,两者都易于打印)。

You can run your project through doxygen. It will index all your functions (and classes, structs etc) and can make them available in multiple formats (including PDF and HTML, both easily printable).

老子叫无熙 2024-07-20 22:37:24

打印函数的最终目标是什么?

你想这样使用它吗:

if (error == Foo())
{
    PrintFunction(foo);
    exit(1);
}

有更简单的方法来输出错误所在的位置。 如果我对您试图解决的问题有更好的了解,我也许可以提供更多帮助。

有关如何实现此类 PrintFunction() 的想法:

  1. 有一个包含函数的数据结构,包含:函数行开始、函数行结束,可能还有指向函数的指针。
  2. 编写一个函数,根据行数打印出一行
    源文件。 __FILE__ 为您提供源文件名。
  3. 知道函数所在位置的开始和结束位置
    代码,打印函数就很简单了。

这有一个恼人的陷阱,需要更新函数在文件中所在位置的行号。 但这也许可以通过宏来解决。

What is your end goal with printing out a function?

Do you want to use this as such:

if (error == Foo())
{
    PrintFunction(foo);
    exit(1);
}

There are easier ways to output where errors are. I could maybe help more if I had a better idea of the problem you are trying to solve with this.

For a idea of how to implement such a PrintFunction():

  1. Have a data struct that wraps around a function and contains: function line start, function line end, and maybe a pointer to the function.
  2. Write a function that prints out a line base on number of the
    source file. __FILE__ gives you the source file name.
  3. With knowing the start and end of where the function lies in the
    code, printing the function would be trivial.

This has an annoying pitfall of needing to update the line numbers of where your function lies in the file. But this could maybe be solved with a macro.

許願樹丅啲祈禱 2024-07-20 22:37:24

我通常在 emacs 中使用 print-region(或者最好是 print-region-with-faces)。 但是,它不是自动化的,我必须手动选择区域。

也适用于其他语言。


以下内容来自评论中的 Tom Smith

(defun print-fn (interactive) 
   (save-excursion (mark-defun) 
   (print-region)))

如果您喜欢此内容,请点击链接到 Tom 的用户页面,然后看看他是否值得你投票......


制作这个CW,所以我不会从人们投票支持汤姆的好想法中受益。 干杯。


澄清后编辑:这似乎没有指出OP的实际问题。 唉。

I generally use the print-region (or preferably print-region-with-faces) from within emacs. However, it is not automated, I have to select the region by hand.

Works in other languages as well.


The following due to Tom Smith in the comments:

(defun print-fn (interactive) 
   (save-excursion (mark-defun) 
   (print-region)))

If you liked this, follow the link to Tom's user-page and see if he deserves your vote...


Making this CW, so I won't benefit from people voting up Tom's good thinking. Cheers.


Edit after clarification: This doesn't seem to be pointed at the OP's actual question. Alas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文