tags:

views:

272

answers:

3

I have a number of C source files(both .c and .h files). header files contains a number of functions. Out of those functions, only partially are used in a source .C file.Suppose a.h,b.h are header files and a.c and b.c are .c files. a.h is included in a.c. But only a number of functions those are in a. h are used and rest are not used. After compilation I find following warnings:

 function XXXX defined but not used.

But those XXXX functions which are not used in a.c are used in b.c. So, i can't completely remove those functions too. So , i decided to make a separate file containing only those XXXX functions and included it wherever it is used.Doing this is creating multiple number of header files. Can anybody please suggest me to some effective way to solve this problem.

+3  A: 

If you just want to hide the warning, use:

-Wno-unused-function

However, you should probably follow the advice in caf's answer instead. It sounds like you may have defined a function when you only meant to add its declaration.

thomasrutter
From my reading, I would guess that these warnings are very valid and should not be hidden. I think Andrey and caf are correct in that the OP is defining static functions in .h files. Simply hiding these warnings will not be helpful; chances are that this will cause larger problems down the road.
Matt B.
You're right, I think that caf's answer above is probably correct. http://stackoverflow.com/questions/2845748/function-defined-but-not-used-warning-in-c/2846030#2846030 I've edited mine now.
thomasrutter
+4  A: 

It sounds like your problem is that you're defining functions in .h files. Don't do that. Instead, just put your declarations in the .h file, and have a matching .c file that contains the function definitions:

common.h:

#ifndef _COMMON_H
#define _COMMON_H

int foo(int a, int b);

int bar(double x, double y, double z);

#endif /* _COMMON_H */

common.c:

#include "common.h"

int foo(int a, int b)
{
    /* code */
}

int bar(double x, double y, double z)
{
    /* code */
}

Then your a.c and b.c should #include "common.h", and you'll need to arrange to have common.c compiled into the complete program.

caf
+6  A: 

"Function defined but not used" warning is only issued for functions with internal linkage, i.e. functions that are declared as static. These functions are only accessible in one translation unit, so the compiler always knows whether they are used (in the program) or not. If you don't reference these functions in their translation unit, these functions are known to be unused, and the warning is generated.

You are saying that these functions "are not used in a.c, but used in b.c". This is not true. When you declare (and define) a function as static in the header file, each translation unit gets its own "copy" of the function. These are completely different, completely independent functions. The fact that they have the same name means nothing to the compiler. So, in b.c you got a completely independent copy of the function, which is used (as you say), but the completely independent copy in a.c is still not used.

The question in this case is why you are doing this. Why on Earth are you defining static functions in the header file? If you really need to do this, you can work around the warning by using some compiler-specific means. Like in GCC, for example, you can declare the function with __attribute__((unused)) an the warning for this function will not be issued.

But normally, one wouldn't need to define functions in the header file. Normally, one'd create a function with external linkage, define it in one of the .c files and put the declaration (prototype) in the header file. The compiler will not issue any warnings in this case, even if the function is declared but not used in some translation unit.

AndreyT