什么是 __attribute__((unused)) 静态?

What is __attribute__((unused)) static?

libuv文件heap-inl.h中,我看到了下面的宏

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

heap-inl.h 包含在源文件 loop.c 中,然后使用声明的函数 heap_init.

根据我的解释...

我不明白的是为什么导出函数被标记为 __attribute((unused))__。另外,为什么它也是一个 static 声明?我以为 static 函数只能在定义它的文件中使用。此外,内联与这些有什么关系?

static关键字表示该函数是编译文件的局部函数。当它在 header 中时,表示它包含在所有编译文件中。 那么问题是,如果不使用此功能,某些编译器(clang、gcc、VS 以及适当的文件)将生成警告。将函数标记为未使用将删除此警告(如果警告被视为错误,则可能会出现错误)。

所以 HEAP_EXPORT 并没有真正导出任何东西,只是使函数可用(如果 body 也在 header 中,如果文件名为 - inl,其实就是表示在编译后的文件中会内联该内容)。

如所述here

unused

This attribute, attached to a function, means that the function is meant to be possibly unused. GCC does not produce a warning for this function.

此属性还有一个额外的好处,即根据具体情况,如果函数从未被调用,它可能根本不会发出(它不会在编译文件中使用 space)。

这通常与头库中的 static 函数一起使用,因此只有实际使用的函数才会作为机器代码发出,并且避免了警告。