开发者

Separating functions to a separate header that rely on another function not inside the header?

开发者 https://www.devze.com 2023-01-01 18:57 出处:网络
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

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?


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
}


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消