C99 inline semantics with gcc (mspgcc)

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

Reading here and using the second built-in parameter c99 with built-in all declarations and definitions, for example:

extern inline void SPFD54124B_write_cmd(uint16_t command); 

in title and

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

in the corresponding file c. I was expecting to get inline versions of features.

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] 

I'm not sure I understand the various alternatives in c99.

+4
source share
2 answers

You have something completely different from what you need. In the header you should use:

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

In translation units that include this header, this will create an inline function with external linkage. You should also place an ad in one of these translation units:

 extern void SPFD54124B_write_cmd(uint16_t); 

This (along with the inline definition from the header) will create an external function definition. Other files that contain a header but do not include an extern declaration will create a built-in function definition: the definition is available only in this translation unit, but this does not prohibit an external definition elsewhere.

In total, you will get one external definition of the function, and each file containing the header will also have an external definition; the compiler can use either. It is clear that in the full program there is still only one function called SPFD54124B_write_cmd - for example, if you take the address of a function in several translation units, you should get the same value.

Alternatively, you can put this in the header:

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

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


(For the children it should be noted that the current default mode GCC "gnu89", which does not implement the C99 semantics for inline )

+6
source

To ensure that the ACTUALLY function can be built in, quite a lot needs to be defined in the header file.

If you do not, most compilers will not actually enable this feature.

So my suggestion was to move the actual content of the function to the header file:

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

Source: https://habr.com/ru/post/1487775/


All Articles