Search code examples
cgccgnulibuv

What is __attribute__((unused)) static?


In libuv file heap-inl.h, I see the following macro

#if defined(__GNUC__)
# define HEAP_EXPORT(declaration) __attribute__((unused)) static declaration
...
HEAP_EXPORT(void heap_init(struct heap* heap));
...

heap-inl.h is included in a source file loop.c that then uses the declared function heap_init.

From what I interpret...

  • heap-inl.h stands for heap "inline"?
  • HEAP_EXPORT is exporting a function to be used by other source files.

What I don't understand is why an exported function is marked __attribute((unused))__. Also, why is it also a static declaration? I thought static functions can only be used in the file it is defined in. Also, what does in-lining have to do with any of this?


Solution

  • The static keyword indicates that the function is local to the compiled file. When it's in a header, it means that it is included in all compiled files. Then the issue is that if this function is not used, some compilers (clang, gcc, VS with the proper files) will generate a warning. Tagging the function as unused will remove this warning (and potential error if the warning is considered as an error).

    So HEAP_EXPORT is not really exporting anything, just making the function available (if the body is also in the header, which is the case if the file is named -inl, which is indeed to indicate that the content will be inlined in a compiled file).