Search code examples
cgcc

Disable static in inline warning in gcc?


I want to disable the warning gcc gives me without using -w. How? Below shows an example of how to with clang

#ifdef __clang__
#pragma GCC diagnostic ignored "-Wstatic-in-inline"
#else
//What do I write for gcc?
#endif
static int data[]={1,2};
inline int test() {
    return data[0];
}

int main() {
    return test() - 1;
}

Solution

  • 6.7.4.3:

    An inline definition of a function with external linkage shall not contain a definition of a modifiable object with static or thread storage duration, and shall not contain a reference to an identifier with internal linkage.

    The warning is correct. But GCC & clang developers have decided that if you explicitly say that you want inline function also to have an external linkage too (and reference static object) to do not emit this warning as it indicates that you know what you are doing.

    Do not silence the warning - only give the compiler a hint that this construct is intentional

    So declare it as extern if you want an external linkage or static if you do not want to.

    static int data[]={1,2};
    extern inline int test() {
        return data[0];
    }
    

    https://godbolt.org/z/vqzW335x4