Does it matter to have a single built-in function in the C file?

I see this example where I have a file crc32.c

that 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);

      

The keyword inline

doesn't work for me because the function must be declared in the header file, not in the C file. Is that correct?

+3


source to share


2 answers


You're right, whoever wrote this code puts things wrong. The header file must contain the inline

function along with its complete definition, and the file .c

must contain a declaration extern inline

without a definition:

Title:

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

      



File C:

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

      

The header will allow you to insert the function code; the file .c

will instruct the compiler to emit an externally visible symbol for it.

+6


source


They forgot the magic word static. Now, if compiled by gcc, it will be ignored and the compiler will decide how and how to implement this feature. At any rate, even if the inline keyword is absent, the compiler (and the linker, if link time optimization is enabled) decides how to implement the function call - by insertion or traditional call.

If you want to make sure a function will be inlined or not, you must use the correct compiler or pragma attributes.



where to place and how to declare inline functions depends on the compiler, optimization, link time optimization and other factors.

Answering your question, if a function should only be visible in the scope of a specific .c file - yes it is, but without the static keyword it will also be placed in the object file.

+1


source







All Articles