Search code examples
cinlinec99

Is it relevant to have a single inline function alone in a C file?


I am seeing this example where I have a crc32.c file which contains:

inline int32_t crc32_compute(int32_t *buffer, size_t size) {
   ...
}

In the header file I find:

extern inline int32_t crc32_compute(int32_t *buffer, size_t size);

To me the inline keyword has no effect because the function should be declared on the header file not on the C file. Is it that correct?


Solution

  • You are correct, whoever wrote this code put things the wrong way around. The header file should contain inline function, along with its full definition, while the .c file should contain extern inline declaration, without a definition:

    Header:

    inline int32_t crc32_compute(int32_t *buffer, size_t size) {
       ...
    }
    

    C file:

    extern inline int32_t crc32_compute(int32_t *buffer, size_t size);
    

    The header will allow the function code to be inlined; the .c file will instruct the compiler to emit an externally visible symbol for it.