问题
I have complete project in C, which can be built with gcc or Visual Studio. There are no calls to external libraries.
I would like to know how many functions there is in that project.
There are no unused functions in source code, and the project comes with tests which run it with different params, so for a dynamic approach (e.g. run-time call tree) I would need to accumulate results after each test.
Are there any tools that can perform static or dynamic analysis?
回答1:
With gcc
:
$ nm elf_file | grep "T " | grep -v " _" | wc -l
Note that gcc
can inline some functions even with optimizations disabled, so you should compile with:
-O0 -fno-builtin -fno-inline-functions-called-once
(-finline-functions-called-once
is enabled by default even in -O0
)
回答2:
The definition of functions might be not as simple as you believe. In particular, functions in the C source code do not match functions in the generated assembly code, notably because of inlined functions.
The number and size of functions depend upon the optimization level and the compiler.
And many headers (e.g. <stdio.h>
) could contain inline functions. If they are not called, the compiler probably would optimize by not emitting their code (and same could be true with some static functions). Quite often, functions declared static inline
do not appear in the optimized code.
Likewise, some macros might get expanded into some function definitions....
If compiling with a sufficiently recent version of GCC (e.g. 4.7 or newer) you might make a simple MELT extension for that purpose. The biggest advantage is that it works inside GCC so will really count what GCC is doing... (e.g. after some optimizations).
Notice that GCC might generate or remove some functions during optimizations (function cloning, function inlining....)
GCC MELT is obsolete since 2017
回答3:
Frama-C has a metrics
plug-in that among other things computes the number of functions in your program. Just use
frama-c -metrics file1.c file2.c ... filen.c
to get as output something like:
Global metrics
==============
Sloc = 2080
Decision point = 117
Global variables = 51
If = 117
Loop = 22
Goto = 75
Assignment = 613
Exit point = 242
Function = 841
Function call = 871
Pointer dereferencing = 447
回答4:
You can also use ctags and parse the tag file. A quick way would be to do something like :
ctags -R -f - . | grep -w f | wc -l
at the root directory of your project but this might not be always correct, for instance if you have a global variable like this somewhere in your tree:
int f;
回答5:
A tool for static analysis of C source code is Edison Design Group. It is the compiler front end and you can leverage it to calculate all sort of metrics for your code.
回答6:
i have an idea to count the functions .c file.. wecan count this .java file also. this is c# class file and you can pass the file to the the class
class CountFunctions
{
private FileInfo File;
private int functions = 0;
public int getNumberOfFunctions
{
get { return functions; }
}
public countfunctions(FileInfo fs) {
File = fs;
getLineOfCode();
}
private void getLineOfCode()
{
string line;
try
{
StreamReader reader = File1.OpenText();
while (true)
{
line = reader.ReadLine();
if (IsFunction(line))
{
Functions++;
}
if (line == null)
break;
}
}
catch (Exception r)
{
Console.WriteLine(r.Message);
}
}
private bool IsFunction(string line)
{
if (line.Contains("void") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
{
if (!line.Contains(";"))
{
return true;
}
}
return false;
}
}
来源:https://stackoverflow.com/questions/18889713/counting-functions-in-c-source-code