I just discovered a missing function in one C API I use (it compiles but it doesn't link). I wanted to test the entire set of API functions. The idea was to keep things simple such as:
include <myapi.h>
void main(void) {
func1;
func2;
...
}
But this compiles and links fine because the compiler (gcc) optimizes away the unused function pointers. If I use func1() instead, I get a linker error if that function is indeed missing. But all the function calls are complex and it would take days to write explicit parameters for all of them.
I could use nm to query the libraries, but first it's a whole set of .so files and second it probably depends on #defines in the API header.
Is there a simple syntax I can use in a C file ? Or maybe just an (un)optimization option for gcc ?
One possible workaround could be to use function pointers to fake usage:
include <myapi.h>
typedef void Func_T(void); // Function type for easier handling
Func_T * volatile Func; // Function pointer where functions are stored
/* Macro to do the assignment */
#define TEST(f) Func = (Func_T*)f
int main(void) {
TEST(func1);
TEST(func2);
...
}
Linker might still remove the functions, but it's worth the try.
Compilers often offer attributes or pragmas to force keeping symbol. It might be useful for keeping Func
if linker tries to remove it.