//在Linux源码的很多头文件中,经常看到static inline函数的定义
static inline struct proc_inode *PROC_I(const struct inode *inode)
{
return container_of(inode, struct proc_inode, vfs_inode);
}
//inline函数提示编译器进行内联,但不保证一定内联
//inline函数对外部不可见,因此可以在多个.c源文件中定义同一个inline函数
//The inline definition that does not use extern is not externally visible and does not prevent other
//translation units from defining the same function.
static inline void exit_aio(struct mm_struct *mm){}
inline#如果没有内联,则程序中调用该函数的地方,将报错undefined reference
static inline#不保证内联,但还有一个static函数
extern inline#不保证内联,但是还有一个全局函数
//main.c
inline int exit_aio(){
return 1+2;
}
int main() {
printf("retval is:%d\n", exit_aio());
}
//gcc main.c -O2 -o main(ok)
//gcc main.c -o main(err,undefined reference to 'exit_aio')
//这样编译也是可以的
inline __attribute__((always_inline)) int exit_aio(){
return 1+2;
}
static inline int exit_aio(){
return 1+2;
}
extern inline int exit_aio(){
return 1+2;
}
https://godbolt.org/
https://elinux.org/Extern_Vs_Static_Inline
https://kernelnewbies.org/FAQ/ExternAndStaticInlineVariable
https://yarchive.net/comp/linux/gcc_inline.html
//extern "C"是c++的语法,C语言没有这个语法
//表示{}中的函数是使用C语言写的(.c文件),可以在C++中被调用,同时对这些函数禁用重载,函数名是唯一标识符
extern "C" {
void test();
}
extern "C" void foo(int);
extern "C" int g();
int g();//OK, has C language linkage
int h();//has C++ language linkage by default
extern "C" int h();//Error: different language linkages
#ifdef __cplusplus
extern "C" int foo(int, int);//C++ compiler sees this
#else
int foo(int, int);//C compiler sees this
#endif
https://en.cppreference.com/w/cpp/language/language_linkage
https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c
https://www.microforum.cc/blogs/entry/34-using-extern-c/