How to implement and share an inlined function using C99?
With gnu89:
/* share.h */
extern inline void f (void);
/* function.c */
void f (void) {}
/* main.c */
#include "share.h"
int main (int argc, char **argv) {
f ();
return 0;
}
With C99:
/* share.h */
static inline void f (void) {}
/* main.c */
#include "share.h"
int main (int开发者_如何学Go argc, char **argv) {
f ();
return 0;
}
How to implement one definition of f()
in function.c
like in gnu89 but using C99 mode?
You put the inline definition the header file without extern
and add an extern declaration/prototype in a source file:
/* share.h */
inline void f(void) {}
/* function.c */
#include "share.h"
extern void f(void);
/* main.c */
#include "share.h"
int main(int argc, char *argv[])
{
f();
return 0;
}
See http://www.greenend.org.uk/rjk/2003/03/inline.html for more information about inline
in C.
If you really want to keep all your definitions (inline or not) in function.c like you say:
/* share.h */
#define WANT_INLINE 1
#include "function.c"
/* function.c */
#ifdef WANT_INLINE
inline void f(void) {}
#else
#include "share.h"
extern void f(void);
#endif
Not heavily tested and not recommended.
精彩评论