how can I find out all the unused functions?

B

bugzilla

hi, all.

I want to know which flag can be used to give me a warning that one
declared function is not called in the program. for example:

#include <stdio.h>
int Nouse();

int main()
{
printf("Hello,world!");
}

int Nouse()
{
printf("I am used!");
}

I want gcc can tell me that althoug int Nouse() was declared and
implemented, but it is not called in the program. I tried to use
-Wall, but it doesnot work.

thanks.
 
M

Martin Ambuhl

bugzilla said:
hi, all.

I want to know which flag can be used to give me a warning that one
declared function is not called in the program. for example:

This is completely compiler-dependent. Such questions usually belong in
compiler-specific newsgroups.
#include <stdio.h>
int Nouse();

int main()
{
printf("Hello,world!");

main returns an int; it is good practice to do so.
}

int Nouse()
{
printf("I am used!");

You claim that Nouse returns an int; it should do so
}

I want gcc can tell me that althoug int Nouse() was declared and
implemented, but it is not called in the program. I tried to use
-Wall, but it doesnot work.

<ot>
Again, this is compiler-specific. I'll let you in on a secret, though,
if you don't tell anyone I answered an off-topic question. Most
versions of gcc won't warn you of unused functions unless they are
static. That is because a function which has external linkage may be
used in another part of the program not in this file. If you mark the
function as static, -Wunused-function or -Wunused might do the job.
</ot>
 
K

Keith Thompson

I want to know which flag can be used to give me a warning that one
declared function is not called in the program. for example:

#include <stdio.h>
int Nouse();

int main()
{
printf("Hello,world!");
}

int Nouse()
{
printf("I am used!");
}

I want gcc can tell me that althoug int Nouse() was declared and
implemented, but it is not called in the program. I tried to use
-Wall, but it doesnot work.

There's no reason to issue a warning for the above code (at least not
for the lack of a call to Nouse(). Nouse isn't called from within
that file, but it could be called from another separately compiled
source file.

If Nouse were declared static, a warning would be appropriate, since
the function would be visible only from that source file. gcc comes
with extensive documentation, which should tell you how to get it to
generate such a warning if it doesn't do so by default. (One hint:
you can sometimes get more warnings at higher optimization levels; the
analysis the compiler has to do to perform optimizations can reveal
problems that aren't detected at lower levels.)

(You probably can expect warnings for the failure to return values
from functions declared to return int. It's also better to declare
"int Nouse(void)" rather than "int Nouse()"; likewise for main.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top