tags:

views:

248

answers:

3

On a program of me, the splint checker warns:

expat-test.c:23:1: Function exported but not used outside expat-test: start
  A declaration is exported, but not used outside this module. Declaration can
  use static qualifier. (Use -exportlocal to inhibit warning)
   expat-test.c:38:1: Definition of start

The start() function is used. The program uses the expat XML parser which works with callbacks. You give the parser a function:

XML_SetElementHandler(parser, start, end);

and the parser calls it back at some points. This is a very common idiom in C and I wonder why splint complains. I find nothing in the FAQ or in the manual.

+2  A: 

Do you call XML_SetElementHandler() in the same translation unit (normally the .c source file) in which start() is defined? If so, the warning might be correct: Add static to the function definition and check if your application still links without errors.

Christoph
Thanks, the start() function was indeed in the same file and adding "static" solved the problem. splint is now happy and the application still works.
bortzmeyer
And I just noticed that splint's warning message gives a hint about "static". Shame on me.
bortzmeyer
+2  A: 

The 'static' keyword effectively hides the name of a function from other translation units (.C file, usually). The code's still there, and from that C file you can get the address of the function (but not from other C files). You can then pass the address to other translation units, either by calling functions, or returning the address from a function, or by storing it in a global variable, etc.

Roger Lipscombe
You should clarify that you can only get the address of the (static) function in the source file that it is defined in; there is nothing to stop you passing the address of that function to code outside the source file it is defined in.
Jonathan Leffler
A: 

I tend to declare all functions which are not being exported as static. I have been taught and currently believe that it is good practice to do so. (Disclaimer: As with most things, there are numerous exceptions to this 'rule'.)

Anthony Cuozzo