C语言中的inline

//在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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值