不幸的是,并非所有编译器都完全符合 C99,即使他们声称会这样做。
一种符合要求的方法是
// header file. an inline definition alone is
// not supposed to generate an external symbol
inline void toto(void) {
// do something
}
// in one .c file, force the creation of an
// external symbol
extern inline void toto(void);
例如,较新版本的 gcc 可以正常工作。
您可以通过定义类似的东西来为其他编译器(伪装者)摆脱它
#ifdef PRETENDER
# define inlDec static
# define inlIns static
#else
# define inlDec
# define inlIns extern
#endif
// header file. an inline declaration alone is
// not supposed to generate an external symbol
inlDec inline void toto(void) {
// do something
}
// in one .c file, force the creation of an
// external symbol
inlIns inline void toto(void);
编辑:
-std=c99
我知道的支持 C99 的编译器(通常是 option )
- gcc (versions >= 4.3 IIRC) 实现了正确的
inline
模型
- pcc也是正确的
- ggc < 4.3 需要一个特殊的选项来实现正确的模型,否则他们使用自己的模型,如果你不小心会导致多个定义的符号
- 如果您不特别注意,icc 只会在每个单元中发出符号。但是这些符号是“弱”符号,所以它们不会产生冲突。他们只是炸毁了你的代码。
- opencc,AFAIR,遵循旧的 gcc 特定模型
- clang 根本不会为
inline
函数发出符号,除非您有extern
声明并且在一个编译单元中使用函数指针。
- tcc 只是忽略
inline
关键字