C99 inline关键字
C99 inline
一直以来都用C++用得比较多,这个学期做操作系统的课设用回了C,结果一波內联函数居然链接不过去……查了查资料,C99引入的inline和C++的inline语义区别是很大的,我算是踩了这个坑。
C++的inline除了建议编译器把函数内容内联以外,主要的作用就是能够让你把一个函数的定义在不同的编译单元里重复,而不会报链接错误。C99的inline则不然,它的语义是:
Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit. If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition.
大概可以总结为以下几点:
- Internal linkage的函数总可以用inline修饰,C代码中常见的static inline用法就是从这来的。
- 在某个编译单元中,如果某个inline函数的任意一个declaration都没有用extern修饰,那么这个编译单元中的该函数定义称为内联定义。编译器可以选择使用内联定义(即接受建议),也可以不使用该定义,此时相当于这个定义不存在(即这个函数的调用需要链接其他编译单元的符号)。
- 内联定义实际上是提供了对外部链接函数的一个“替代方案”。比如你在a.c中已经有了函数foo的定义,但是你在b.c中又给了个foo的内联定义,那么编译器可能会用b.c中给的内联定义,也可能视之不见。
所以C语言的inline语义的正确使用方法应该有下面三种:
static inline,不解释:
// a.h
static inline int func(int x) { /* ... */ }
这样做可以模仿C++的inline语义:
// a.h
inline int func(int x) { /* ... */ }
// a.c
extern int func(int x);
提供函数的“内联版本”,由编译器进行选择:
// a.h
int func(int x);
//a.c
int func(int x) { /* implementation [1] */ }
// b.c
inline int func(int x) { /* implementation [2] */ }
void A(void)
{
//...
i = func(j);
//...
}
最后一种用法中,implementation [1]和implementation [2]可以是不一样的。也就是说,我们可以为已有的函数提供一个“内联版本”,这个版本不需要和原版相同。至于用哪个,则由编译器决定。
C99 inline关键字的更多相关文章
- inline关键字的作用
一.在C&C++中,inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 如下面一宏定义表达式: #define express(v1,v2) (v1 ...
- C++中的inline关键字
from here 1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程 ...
- inline关键字的用法详解
1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程序的局部数据(也就是 ...
- (C\C++)inline关键字
背景(C&C++中) inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义如: #define ExpressionName(Va ...
- (C)inline关键字
背景(C&C++中) 一.inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例:#define ExpressionNam ...
- C++ 内联函数 inline关键字
inline 关键字主要功能是为了 代替掉 宏代码片段. 在C++中使用关键字inline关键字声明内联函数. inline int fun(int a,int b) { return a < ...
- 转: inline关键字使用
1.inline用在函数声明时,还是函数定义时?还是两边都加? 首先,内联函数声明和定义最好在同一个文件中,其它的情况没有实用上的意义. 只要在同一个文件中,声明和定义至少其一加"inlin ...
- c++ inline关键字的理解
1. inline是实现修饰符,而非声明修饰符,所以应该用于实现部分的修饰(你也可以放置inline在声明,但是没有必要) 2. 所有中类中定义的函数都默认声明为inline函数,所有我们不用显示地去 ...
- inline 关键字
inline 定义一个函数时最好前面再加上static: static inline 定义的函数,会在链接阶段将代码段中没有使用的inline 函数定义“剔除”,从而减小编译体积:即使加了-g参数仍然 ...
随机推荐
- DataFactory连接MySQL数据库
1.下载驱动 https://dev.mysql.com/downloads/connector/odbc/ 需要使用oracle登录账号密码后才能下载 下载完成后进行安装,一路下一步即可 2.连接m ...
- 1.ibatis核心类
- 修改vscode终端样式
在设置中查找workbench,然后编辑setting.json: "terminal.integrated.cursorBlinking": true, "termin ...
- LC 794. Valid Tic-Tac-Toe State
A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...
- Callable和Supplier的区别
A Callable is "A task that returns a result, while a Supplier is "a supplier of results&qu ...
- LVS系列二、LVS集群-DR模式
一. LVS-DR和LVS-IP TUN集群概述 1. Direct Routing(直接路由) Director分配请求到不同的real server.real server处理请求后直接回应给用 ...
- Kafka 和 ZooKeeper 的分布式消息队列分析
1. Kafka 总体架构 基于 Kafka-ZooKeeper 的分布式消息队列系统总体架构如下: 如上图所示,一个典型的 Kafka 体系架构包括若干 Producer(消息生产者),若干 bro ...
- nodeslector使用
问题: node节点挂了一个, 无法切换到另一个node上 解决: .指定了 nodeslector .设置了下面: hostNetwork: true dnsPolicy: ClusterFirst ...
- 用js的方式运行c程序之webassemly
在这里就不科普webassemly的作用以及好处了,请自行百度. 那么,怎么通过js的方式在浏览器中运行c程序呢,其中原理如下: 可能另一张图会更详细: 1.安装emscripten 说明文档地址:h ...
- JS小时倒计时
let t1 = new Date("2019-11-26 15:51:00");// 从什么时间开始 let t2 = ));// 延迟几个小时 let interval = w ...