Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global inline function between two c files

Following from this question I asked before. I want to know how could I define an inline function in global scope.

I want to declare inline function in test.h, define it in main.c and call it from test.c.

main.c and test.c both #include "test.h" (for code sample please click the link above).

This is basiclly some sort of callback function that user can enable/disable. And only one file should have the function defined.

I know inline is just a suggestion to the compiler and it doesnt make much difference on modern CPUs but this is for 8-bit microcontroller and would really need it.

EDIT:

I have a function in test.c which calls this inlined function. I just want to replace the call with the body of function defined in main.c. I hope that makes sense.

like image 529
user1806687 Avatar asked Aug 08 '26 13:08

user1806687


2 Answers

Most 8-bit microcontroller code is written in language that approximates C89 (there are often extensions, e.g. inline assembly), and inline was not officially part of C until C99. So first of all identify what standard you are compiling for (if any) and consult your compiler manual if inline is an extension (i.e. not C99 inline). Compiler manuals take precedence over C standards in that realm of programming.

Per C99 If both main.c and test.c both need to call the same function AND it needs to be marked inline, then I believe it MUST be defined in the header because an inline function cannot span translation units (i.e. ".c" files).

For example:

test.h:

static inline int add(int a, int b) { return a + b; }

main.c:

#include "test.h"
void main(void)
{
   int x = add(10,15);
}

test.c:

#include "test.h"
int test(void)
{
   int x = add(10,15);
   return x;
}

EDIT: See here for better explanation of C99 inline usage: How to declare an inline function in C99 multi-file project?

However, this can cause all sorts of weird behavior with the resulting object code. For example, I've seen a compiler from a major MCU mfg (who will intentionally go unnamed) generate object code for inline functions #included in a given translation unit instead of inlining them. The file was included in several places and contained many functions so this caused a massive increase in overall ROM usage throughout the code base because the linker was also incapable of removing dead code (see --gc-sections for gcc) AND failed to improve performance because the functions were never actually inlined, even though it was technically the correct use of inline.

Our solution to this particular problem was to convert all the functions to macros (which gave the perfomance benefit intended with inline) but an alternate approach would have been removing the inline from the declaration and moving the definiton to a single .c file.

TL;DR: If using inline for MCUs, 1) consult your compiler manual, and 2) keep an eye on object code generated.

like image 57
Brian McFarland Avatar answered Aug 11 '26 05:08

Brian McFarland


Assuming you mean what you say, that the function in question is a callback, you can't inline a callback. It doesn't make sense, the callback-call usually happens through a function pointer, I don't think you can expect the compiler to prove that its value is constant (and known at compile-time) so that it can replace the call with inlined code.

like image 43
unwind Avatar answered Aug 11 '26 03:08

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!