0

Lets say I have 3 seperate files called: main.c, test.c and test.h. How could I "force" a function that includes test.h to define a function which is already declared in test.h, that test.c file can call it.

This ia a bit messy, so let me give you some code samples of what I mean.

test.h:

...
#define SOMETHING
...
#ifdef SOMETHING
inline void test_func(BYTE b);
#endif
...

test.c:

#include "test.h"

void somefunction(void) 
{
    ...
    #ifdef SOMETHING
    test_func(integer_value);
    #endif
    ...        
}

main.c:

#include "test.h"

int main(void)
{
    ...
}
inline void test_func(BYTE b)
{
    // Do something with b
}

In short: test.h declared a function, main.c defines that function and test.c calls that function.

Is this possible? How?

EDIT:

The user declares the function in main.c when to compiler gives a error or something that that function is not declared.

user1806687
  • 934
  • 1
  • 10
  • 27
  • "How could I "force" a function that includes test.h"... functions don't include headers; translation units do. As you have it now, it will *not* link correctly, as `test_func` is `inline`d in a single translation unit (main.c), yet referenced from another (test.c). What problem are you *really* trying to solve ? – WhozCraig Feb 18 '15 at 19:22
  • @WhozCraig I am writting a piece of software for microcontrollers that the users will be able to use. But I need them to manually define that specific function, because its up to them to do with that value what they want. – user1806687 Feb 18 '15 at 19:24
  • Would a function pointer, referencing a default implementation or detectable as NULL (no-op) and reset to the user-provided alternative on load achieve your goal ? – WhozCraig Feb 18 '15 at 19:26
  • @WhozCraig But wouldn't this require a global variable in test.h? – user1806687 Feb 18 '15 at 19:27
  • @WhozCraig Could you post an anwser with what exactly you mean? I am having trouble understanding. – user1806687 Feb 18 '15 at 19:29
  • Likely it would require an `extern` setup in one way or another. If you want a compile-time solution the alternative is to lose the inlining and instead simply prototype and invoke. The linker will sort out the madness if the user didn't `#define SOMETHING` and provide an implementation during build. – WhozCraig Feb 18 '15 at 19:30
  • @WhozCraig I guess I could live without inline. – user1806687 Feb 18 '15 at 19:32
  • @WhozCraig Just for completness could you provide more detail on how you'd achieve it, how you said it above ? With extern and what not. – user1806687 Feb 18 '15 at 19:34
  • If you want it compile-time, my prior comment (and dove-tailing with Philip's answer) is how I would do it. Provide the prototype, invoke the function, and mandate the user `#define SOMETHING` and provide a function body *somewhere* (doesn't have to be `main.c`; they could conditionally pull inan additional .c file into the build). I think you have it down. A functor-ptr solution would be more akin to a runtime-decision of execution, and considerably more tedious to setup. – WhozCraig Feb 18 '15 at 19:38

1 Answers1

2

In short it should be possible to do this. Why ? The file implementing a function does not have to be in a header file with the same name. The linker will resolve any issues if any and complain if something is missing. If the compiler complains, then there is a missing header file or your header file function definition does not match with what you have in the c file.

Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39