如何在C99中正确内联和使用内联函数,更正链接故障? [英] How to properly inline and use an inline function in C99, correcting link failure?

查看:90
本文介绍了如何在C99中正确内联和使用内联函数,更正链接故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对以下简化的 example.c 文件执行 cc -std = c99 example.c

Doing cc -std=c99 example.c on the following simplified example.c file:

inline void a()
{
}

int main()
{
   a();

   return 0;
}

得到我:

在函数'main'中:

example.c :(。text + 0x7):未定义对'a'的引用

collect2:ld返回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)

推荐答案

可能

内联函数定义应该放在头文件中,而 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
#include "the-file.h"
extern inline void a(void);

BTW,声明 a 不包含 void 不是原型。

BTW, declaring a without void is not a prototype.

这篇关于如何在C99中正确内联和使用内联函数,更正链接故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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