ccross-platformpreprocessor

Stop compiling if a called function is not supported under OS


I am creating a C library which is supposed to be cross-platform.

For the time being, I have a function:

void example() {
#ifdef __linux__
    // do stuff
#else
    // Not implemented
#endif
}

As you can see above, I have written some code for linux, but other OSes are not supported (as of now at least).

Is there a way to stop compilation ONLY if the function is called?

In other words, what I want to do is to not allow someone to build a project under an unsupported OS ONLY IF this (or another unimplemented function) is called. BUT i want to still be able to build the library for other Operating Systems IF that function is never used.

I can probably just use this:

#ifdef __linux__
void example() {
}
#endif

But I want to show a more descriptive message using pragma/error explaining that this function is not supported. (To avoid confusion on why it doesn't build)


Solution

  • If the real function is void example (void) but not present in the current build, you ought to get linker errors.

    If that's still not good enough and you must have compiler errors, then consider "mocking" the function with a macro generating a compiler error as soon as it is used, but only then:

    #ifdef __linux__
        // example() is present
    #else
        #define example() do { _Static_assert(0, "example() not supported"); } while(0)
    #endif