views:

24

answers:

2

I have several C scripts that will all have the same format(functions), but occasionally the actual code within a few functions. I am trying to separate a function into an external header file, but the issue is this:

int FunctionImExtracting()
{
    //some code
    FunctionThatCannotBeExtractedButTheFunctionSignatureWillAlwaysRemainTheSame()
    //more code.
};

I have a feeling that function pointers will be my friend here, but I'm not sure how. How can this be accomplished?

A: 

You will need a header file with the declaration of FunctionThatCannotBeExtractedButTheFunctionSignatureWillAlwaysRemainTheSame(). Then in the file to which you are exporting, you'll need to include this header. And the actual definition of FunctionThatCannotBeExtractedButTheFunctionSignatureWillAlwaysRemainTheSame() can still be in the file from which you exported FunctionImExtracting().

Did I get your problem right?

Michał Trybus
+1  A: 

You don't (normally want to) put the function itself into a header -- you just want to put a declaration of the function in the header, so other files can include the header and be able to call that function:

//myfunc.h:
#ifndef MY_HEADER_H_INCLUDED
int FunctionImExtracting(void);
#define MY_HEADER_H_INCLUDED
#endif


// myfunc.c:
#include "theirheader.h"
#include "myfunc.h"

int FunctionImExtracting() { 
  // as in question
}
Jerry Coffin