2

I am working on a C library which sometimes uses

 static inline void myfunc(...)

when defining a function.

Now I try to port this to an old C compiler that does not support "static inline". This is bcc - Bruce's C compiler.

Can I use a command in a header file that replaces

static inline void

with

void

in all programs that include this header file?

fuz
  • 88,405
  • 25
  • 200
  • 352
Georg
  • 119
  • 1
  • 10

2 Answers2

5

When you must target a compiler that does not support certain features, it is common to use macros in your code, rather than trying to modify your code with macros.

In this situation you can define STATIC_INLINE macro in a compiler-dependent way, and use it like this:

#ifdef BCC_COMPILER
#define STATIC_INLINE
#else
#define STATIC_INLINE static inline
#endif
...
STATIC_INLINE void myfunc(...)
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • This will work, but I already have so many ifdef's in the code. I hoped it would be shorter to define this in one place with a #define command or similar. – Georg Jan 01 '17 at 13:30
  • I now understand, yes you can put this code into the header file. Is it possible to get the preprocessor to replace "static inline" with an empty string? Then I do not have to change every program code. – Georg Jan 01 '17 at 13:36
  • 1
    @Georg Unfortunately, preprocessor can replace a single token, not a pair of tokens that appear next to each other. That's why many libraries I've seen use this trick, even though it makes the rest of the code less readable. – Sergey Kalinichenko Jan 01 '17 at 14:49
  • 1
    Do you really want to lose the `static` from `static inline`? You're suddenly exposing all the functions and liable to get multiple definition errors. If the compiler doesn't support `inline`, `#define inline /*nothing*/` might be better. (Of course, if the functions that were previously `static inline` are now just `static` but aren't used, you might get warnings about unused functions instead — or suffer code bloat because of them, or both.) – Jonathan Leffler Jan 01 '17 at 18:28
1

Thank you very much to all for the help. I have to report that BLUEPIXY gave the answer that worked for me in his comment:

 #define inline

Apparently bcc does accept static void but not static inline void.

Community
  • 1
  • 1
Georg
  • 119
  • 1
  • 10
  • 1
    A simpler way of expressing that would be 'Bcc does not support `inline` functions'. The support for `static` and `void` is required even in C90 compilers; `inline` was added to C99. – Jonathan Leffler Jan 01 '17 at 18:31