C内联函数的内外链接的区别

1.内联函数的内链接如inline static void fn(void) {} 没有任何限制(建议使用)

2.内联函数的外链接如inline void fn(void) {} 则有诸多限制,最易被忽略的便是内联函数的外链接的定义(不仅需要.h文件的替换体,还需要单独的.c文件存放extern inline void fn(void)的外部定义);另外,一个非 static 的内联函数不能定义一个非 const 的函数局部 static 对象,并且不能使用文件作用域的 static 对象。例如:

static int x;
inline void f(void) { static int n = 1; // 错误:非 const 的 static 对象在非 static 的 inline 函数中 int k = x; // 错误:非 static 的 inline 函数访问 static 变量 }

示例代码:

// file test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
inline int sum (int a, int b) { return a+b; } #endif   // 文件 sum.c #include "test.h" extern inline int sum (int a, int b); // 提供外部定义   // 文件 test1.c #include <stdio.h> #include "test.h" extern int f(void);   int main(void) { printf("%d\n", sum(1, 2) + f()); }   // 文件 test2.c #include "test.h"   int f(void) { return sum(2, 3); }

转载于:https://www.cnblogs.com/catgo/p/9982345.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值