如何正确内联和使用C99内联函数? (构建失败) [英] How to properly inline and use an inline function in C99? (build fails)

查看:587
本文介绍了如何正确内联和使用C99内联函数? (构建失败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CC -std = C99 example.c 以下简化 example.c 文件:

inline void a()
{
}

int main()
{
   a();

   return 0;
}

让我:

在函数'主':结果
  。example.c :(文字+为0x7):未定义引用'A'结果
  collect2:劳工处返回1退出状态

In function `main':
example.c:(.text+0x7): undefined reference to 'a'
collect2: ld returned 1 exit status

据我所知,这与C99标准的要求做,要求只有一个多定义为的情况下使用内联的每个非静态函数,其中身体不能被内联?如果是这样的话,我猜测,我可以用静态内嵌做代替,但我不希望这以后咬我,所以这将是最好的行动过程这里?很显然,我要坚持到C99,我要内联的一些功能。 (是的,我知道的编译器通常都知道什么直列没有被告知的话,但我有我的理由)

As I understand this has to do with the requirement of C99 standard to demand exactly one more definition for each inline non-static function that is used in cases where the body cannot be inlined? If that is so, I am guessing I could do with static inline instead, but I don't want this to bite me later, so what would be the best course of action here? Obviously, I want to stick to C99 and I want to inline some functions. (Yes, I am aware the compiler usually knows what to inline without being told so, but I have my reasons)

推荐答案

也许你不会有一个错误,当您使用-O2左右编译。

Probably you wouldn't have that error when you compile with -O2 or so.

内联函数的定义应该在头文件和 extern内联声明的应该去的有一个的编译单元。做

Inline function definitions should go in header files and an extern inline declaration should go in one compilation unit. Do

inline void a(void){
 // empty
}

// in just one .c file
extern inline a(void);

BTW,宣告 A 没有无效不是一个雏形。

这篇关于如何正确内联和使用C99内联函数? (构建失败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆