使用gcc的c99内联语义(mspgcc) [英] c99 inline semantics with gcc (mspgcc)

查看:78
本文介绍了使用gcc的c99内联语义(mspgcc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写几个我想内联的函数.

I am writing a couple of functions that I would like to inline.

此处中阅读并使用第二个c99内联选项并启用内联所有声明和定义,例如:

Reading here and using the second c99 inline option with inline on all declarations and definitions, like so:

extern inline void SPFD54124B_write_cmd(uint16_t command);

在标题中,

inline void SPFD54124B_write_cmd(uint16_t command)
{
    spi_write(command, CMD_WIDTH);
}

在相应的c文件中.我原本希望获得这些函数的内联版本.

in a corresponding c file. I was expecting to get inlined versions of the functions.

但是当我编译的时候我得到了:

But when I compile i get:

Generating dependencies dep/spi.d from src/spi.c
Generating dependencies dep/spfd54124b.d from src/spfd54124b.c
Generating dependencies dep/pomodoro.d from src/pomodoro.c
Generating dependencies dep/font8x8_ualnum.d from src/font8x8_ualnum.c
Generating dependencies dep/font8x8_basic.d from src/font8x8_basic.c
Generating dependencies dep/evading_util.d from src/evading_util.c
Compiling src/evading_util.c
Compiling src/font8x8_basic.c
Compiling src/font8x8_ualnum.c
Compiling src/pomodoro.c
src/spfd54124b.h:96:20: warning: inline function 'SPFD54124B_write_pixel' declared but never defined [enabled by default]
src/spfd54124b.h:95:20: warning: inline function 'SPFD54124B_write_param' declared but never defined [enabled by default]
src/spfd54124b.h:94:20: warning: inline function 'SPFD54124B_write_cmd' declared but never defined [enabled by default]
src/spi.h:22:20: warning: inline function 'spi_write' declared but never defined [enabled by default]
src/spfd54124b.h:96:20: warning: inline function 'SPFD54124B_write_pixel' declared but never defined [enabled by default]
src/spfd54124b.h:95:20: warning: inline function 'SPFD54124B_write_param' declared but never defined [enabled by default]
src/spfd54124b.h:94:20: warning: inline function 'SPFD54124B_write_cmd' declared but never defined [enabled by default]
src/spi.h:22:20: warning: inline function 'spi_write' declared but never defined [enabled by default]
src/pomodoro.c: In function 'main':
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:29:25: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:31:25: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:35:25: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:36:25: warning: called from here [-Winline]
src/spfd54124b.h:96:20: warning: inlining failed in call to 'SPFD54124B_write_pixel': function body not available [-Winline]
src/pomodoro.c:40:31: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:43:25: warning: called from here [-Winline]
Compiling src/spfd54124b.c
src/spi.h:22:20: warning: inline function 'spi_write' declared but never defined [enabled by default]
src/spi.h:22:20: warning: inline function 'spi_write' declared but never defined [enabled by default]
src/spfd54124b.c: In function 'SPFD54124B_init':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:124:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:124:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_read':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:25:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:29:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_write_cmd':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:124:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_write_param':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_write_pixel':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_setrow':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:124:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_setcol':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:124:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_lputch':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:124:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spfd54124b.c: In function 'SPFD54124B_putch':
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:124:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
src/spi.h:22:20: warning: inlining failed in call to 'spi_write': function body not available [-Winline]
src/spfd54124b.c:129:14: warning: called from here [-Winline]
Compiling src/spi.c
Linking bin/pomodoro.elf

>>>> Size of Firmware <<<<
   text    data     bss     dec     hex filename
   3150       0       2    3152     c50 bin/pomodoro.elf

src/spfd54124b.h:96:20: warning: inline function 'SPFD54124B_write_pixel' declared but never defined [enabled by default]
src/spfd54124b.h:95:20: warning: inline function 'SPFD54124B_write_param' declared but never defined [enabled by default]
src/spfd54124b.h:94:20: warning: inline function 'SPFD54124B_write_cmd' declared but never defined [enabled by default]
src/spi.h:22:20: warning: inline function 'spi_write' declared but never defined [enabled by default]
src/spfd54124b.h:96:20: warning: inline function 'SPFD54124B_write_pixel' declared but never defined [enabled by default]
src/spfd54124b.h:95:20: warning: inline function 'SPFD54124B_write_param' declared but never defined [enabled by default]
src/spfd54124b.h:94:20: warning: inline function 'SPFD54124B_write_cmd' declared but never defined [enabled by default]
src/spi.h:22:20: warning: inline function 'spi_write' declared but never defined [enabled by default]
src/pomodoro.c: In function 'main':
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:29:25: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:31:25: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:35:25: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:36:25: warning: called from here [-Winline]
src/spfd54124b.h:96:20: warning: inlining failed in call to 'SPFD54124B_write_pixel': function body not available [-Winline]
src/pomodoro.c:40:31: warning: called from here [-Winline]
src/spfd54124b.h:94:20: warning: inlining failed in call to 'SPFD54124B_write_cmd': function body not available [-Winline]
src/pomodoro.c:43:25: warning: called from here [-Winline]

我不确定我是否了解c99中不同的内联替代方法.

I'm not sure I understand the different inlining alternatives in c99.

推荐答案

您所遇到的事情与需要的方式完全错误.在标题中,您应该使用:

You have things exactly the wrong way around from the way you need them. In the header, you should use:

inline void SPFD54124B_write_cmd(uint16_t command)
{
    spi_write(command, CMD_WIDTH);
}

在包含此标头的翻译单元中,这将创建具有外部链接的内联函数.在这些翻译单元中的确切一个 中,您还应该放置声明:

In the translation units that include this header, this will create an inline function with external linkage. In exactly one of these translation units you should also place the declaration:

extern void SPFD54124B_write_cmd(uint16_t);

这(与标题中的inline定义一起)将创建函数的外部定义.其他包含标头但不包含extern声明的文件将创建函数的内联定义:该定义仅在该翻译单元中可用,但不禁止在其他地方使用外部定义

This (together with the inline definition from the header) will create an external definition of the function. The other files that include the header but do not include the extern declaration will create an inline definition of the function: a definition only available in that translation unit, but that does not forbid an external definition elsewhere.

总共,您将对函数有一个外部定义,并且每个包含标头的文件也将具有一个非外部定义;编译器可以使用任何一个.从概念上讲,整个程序中仍然只有一个称为SPFD54124B_write_cmd的函数-例如,如果以多个转换单位获取该函数的地址,则应该获得相同的值.

In total you will have one external definition of the function, and each file that includes the header will also have a non-external definition available; the compiler can use either. Conceptually there is still just one function called SPFD54124B_write_cmd in the complete program - for example if you take the address of the function in multiple translation units you should get the same value.

或者,您也可以将其放在标题中:

As an alternative, you can put this in the header:

static inline void SPFD54124B_write_cmd(uint16_t command)
{
    spi_write(command, CMD_WIDTH);
}

,并且完全不使用extern声明;这将在包含头文件的每个文件中创建一个具有内部链接的内联函数.根本就没有该函数的外部定义,并且从概念上讲,每个包含标头的翻译单元都有其自己的函数独立副本.

and use no extern declaration at all; this will create an inline function with internal linkage in each file that includes the header. There will be no external definition of the function at all, and conceptually each translation unit that includes the header has its own independent copy of the function.

(应该为后人指出,GCC当前的默认模式是"gnu89",不是不会实现inline的C99语义)

(It should be noted for posterity that GCC's current default mode is "gnu89", which does not implement C99 semantics for inline)

这篇关于使用gcc的c99内联语义(mspgcc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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