gpt4 book ai didi

c99 内联语义与 gcc (mspgcc)

转载 作者:太空狗 更新时间:2023-10-29 15:20:02 27 4
gpt4 key购买 nike

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

阅读 here并在所有声明和定义上使用带有内联的第二个 c99 内联选项,如下所示:

extern inline void SPFD54124B_write_cmd(uint16_t command);

在标题中,和

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

在相应的c文件中。我期待获得函数的内联版本。

但是当我编译时我得到:

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 中不同的内联选项。

最佳答案

您以完全错误的方式处理事情,而不是您需要它们的方式。在页眉中,您应该使用:

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

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

extern void SPFD54124B_write_cmd(uint16_t);

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

总共你将有一个函数的外部定义,每个包含头文件的文件也将有一个可用的非外部定义;编译器可以使用任何一个。从概念上讲,整个程序中仍然只有一个名为 SPFD54124B_write_cmd 的函数 - 例如,如果您在多个翻译单元中获取该函数的地址,您应该得到相同的值。

作为替代方案,您可以将其放在标题中:

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

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


(后人应该注意 GCC 当前的默认模式是“gnu89”,它内联实现 C99 语义)

关于c99 内联语义与 gcc (mspgcc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17266073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com