Suppose we have the following iterface to a library:
// my_interface.h
typedef float (*myfunc)(float);
myfunc get_the_func();
And suppose that the implementation is as follows:
// my_impl.c
#include <math.h>
myfunc get_the_func() {
return sinf;
}
And now suppose the client code does the following:
#include "my_interface.h"
...
myfunc func = get_the_func();
printf("%f\n", func(0.0f));
Does the standard guarantee that get_the_function() will return the address of the standard math library sinf()? If so, where in the standard is this implied? Please note the sinf() is not explicitly called anywhere.
The standard does not require external functions to be called: being referenced is enough to be kept in the results of translation. According to the standard's section 5.1.1.2.1, during the eights (and last) phase of translation
All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation.
Since returning a pointer to a function is considered a reference to it, the standard guarantees that sinf
will be linked to whatever implementation that you supply to the linker, which may or may not be the one coming from the standard math library.