Jason S
Jason S

Reputation: 189686

declarations for inline static functions

For C99, is the following syntax legal when splitting up static inline functions into separate declarations and definitions?

foo.h:

#include "foo_decl.h"
#include "foo_def.h"

foo_decl.h:

#ifndef _FOO_DECL_H
#define _FOO_DECL_H

/* Here we document foo */
inline int foo(int x);

/* Here we document bar */
inline int bar(int x, int y);

#endif // _FOO_DECL_H

foo_def.h:

#ifndef _FOO_DEF_H
#define _FOO_DEF_H

inline static int foo(int x) { return x+3; }

inline static int bar(int x, int y) { return x-y+22; }

#endif // _FOO_DEF_H

The declaration is really useless to the compiler, but it serves a purpose for collecting documentation for functions in one place without getting cluttered by interspersed implementation details.

Or should foo_decl.h include the static keyword?

Upvotes: 0

Views: 1087

Answers (1)

M.M
M.M

Reputation: 141576

This is undefined behaviour according to 6.2.2/7:

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

The line inline int foo(int x); declares foo to have external linkage (6.2.2/5), but static int foo(int x) { declares foo to have internal linkage.

If you really want to do this then the versions in foo_decl.h and foo_def.h must either both have static, or neither have static. In the latter case exactly one .c file must have extern inline int foo(int x);.

Upvotes: 1

Related Questions