[C高手进] C99 inline为什么一定要用static或extern修饰?

FengPrince 2013-09-05 08:55:53
main.c中定义inline函数f,然后在main函数中调用。
出错:undefined reference to f

这是C标准原话:
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.

让我不明白的是,编译器总是倾向于调用external definition的函数,如果inline函数不加extern使其变成external definition,inline函数压根就没有用!!C99为什么要这样设计呢??

我找了一个帖子,不过也没有完全说清楚
http://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99
...全文
319 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-09-06
  • 打赏
  • 举报
回复
引用 5 楼 supermegaboy 的回复:
[quote=引用 4 楼 AnYidan 的回复:] supermegaboy
到![/quote] 期待标准控专家的权威解释中……
独孤的根号3 2013-09-06
  • 打赏
  • 举报
回复
让制定标准的大牛解释更靠谱
飞天御剑流 2013-09-05
  • 打赏
  • 举报
回复
引用 4 楼 AnYidan 的回复:
supermegaboy
到!
AnYidan 2013-09-05
  • 打赏
  • 举报
回复
supermegaboy
max_min_ 2013-09-05
  • 打赏
  • 举报
回复
有些语法就是两个字:规定 没办法,但是感觉也没必要纠结这些东西!
FengPrince 2013-09-05
  • 打赏
  • 举报
回复
引用 1 楼 taodm 的回复:
搞清楚这个对你有多少实际意义?
说大不大,说小不小。反正该掌握的已经掌握了,随口一问罢了。。。
taodm 2013-09-05
  • 打赏
  • 举报
回复
搞清楚这个对你有多少实际意义?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧