zoukankan      html  css  js  c++  java
  • 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.

    大概可以总结为以下几点:

    1. Internal linkage的函数总可以用inline修饰,C代码中常见的static inline用法就是从这来的。
    2. 在某个编译单元中,如果某个inline函数的任意一个declaration都没有用extern修饰,那么这个编译单元中的该函数定义称为内联定义。编译器可以选择使用内联定义(即接受建议),也可以不使用该定义,此时相当于这个定义不存在(即这个函数的调用需要链接其他编译单元的符号)。
    3. 内联定义实际上是提供了对外部链接函数的一个“替代方案”。比如你在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]可以是不一样的。也就是说,我们可以为已有的函数提供一个“内联版本”,这个版本不需要和原版相同。至于用哪个,则由编译器决定。

  • 相关阅读:
    剑指offer(第2版)刷题 Python版汇总
    git学习笔记
    Python中的lambda、map和filter
    算法题 22 折纸问题 (牛客网,今日头条)
    算法题 21 findNSum (好未来,LeetCode,牛客网)
    算法题 20 或与加
    Python的内存管理机制
    【算法题12 解码方法decode way】
    理解循环神经网络的来龙去脉
    机器学习资源个人汇总
  • 原文地址:https://www.cnblogs.com/AirGuanZ/p/8724002.html
Copyright © 2011-2022 走看看