How to properly inline for static libraries

后端 未结 2 1038
南旧
南旧 2020-12-28 18:43

I\'m re-writing a small C math library of mine that will end up as a static library for the user and would like to benefit from inlining for my vector math interface.

<
2条回答
  •  没有蜡笔的小新
    2020-12-28 19:20

    You should define your function as static inline in the .h file:

    static inline float clampf( float v, float min, float max )
    {
        if( v < min ) v = min;
        if( v > max ) v = max;
    
        return v;
    }
    

    The function must be absent in the .c file.

    The compiler may decide not to inline the function but make it a proper function call. So every generated .o file may contain a copy of the function.

提交回复
热议问题