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

时间:2022-09-06 21:32:35

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.

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

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.

编辑:对不起,我应该更清楚一点我的最终目标。我希望打印到屏幕上的功能,这样我就可以使用wc来获取这个特定功能的字数。此外,我希望这是命令行工具链的一部分,因此它不是手动输入文件和选择文本的选项。

3 个解决方案

#1


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).

您可以通过doxygen运行您的项目。它将索引所有函数(以及类,结构等),并使它们以多种格式(包括PDF和HTML,可轻松打印)提供。

#2


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():

有关如何实现这样的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. 有一个包装函数的数据结构,包含:函数行开头,函数行结束,也许是指向函数的指针。

  3. Write a function that prints out a line base on number of the source file. __FILE__ gives you the source file name.
  4. 编写一个函数,根据源文件的编号打印出一行。 __FILE__为您提供源文件名。

  5. With knowing the start and end of where the function lies in the code, printing the function would be trivial.
  6. 通过了解函数在代码中的开始和结束,打印函数将是微不足道的。

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.

这有一个令人讨厌的缺陷,需要更新函数在文件中的位置。但这可能可以通过宏来解决。

#3


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.

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

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...

如果您喜欢这个,请点击Tom的用户页面链接,看看他是否值得投票......


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

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


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

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

#1


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).

您可以通过doxygen运行您的项目。它将索引所有函数(以及类,结构等),并使它们以多种格式(包括PDF和HTML,可轻松打印)提供。

#2


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():

有关如何实现这样的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. 有一个包装函数的数据结构,包含:函数行开头,函数行结束,也许是指向函数的指针。

  3. Write a function that prints out a line base on number of the source file. __FILE__ gives you the source file name.
  4. 编写一个函数,根据源文件的编号打印出一行。 __FILE__为您提供源文件名。

  5. With knowing the start and end of where the function lies in the code, printing the function would be trivial.
  6. 通过了解函数在代码中的开始和结束,打印函数将是微不足道的。

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.

这有一个令人讨厌的缺陷,需要更新函数在文件中的位置。但这可能可以通过宏来解决。

#3


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.

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

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...

如果您喜欢这个,请点击Tom的用户页面链接,看看他是否值得投票......


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

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


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

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