Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

c - C99 referring to inline function: undefined reference to XXX and why should I put it to header?

Does gcc completely conform to the inline model specified in C99 standard?

I've browsed some info about this problem. But I can't understand why the inline func must be specified with "extern" or "static".

In .c file, calling an inline function defined in the same translation unit causes an error. What's the reason of compiler behaviour?

I found a post Is “inline” without “static” or “extern” ever useful in C99?

What does this mean?

If a call to some function func with external linkage occurs where an inline definition is visible, the behavior is the same as if the call were made to another function, say __func, with internal linkage.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The inline semantic in C99 is a little bit confusing I have to admit. The inline quantifier allows you to define alternative definitions of a function.

If a function is defined everywhere as just inline in both declarations and definition then this definition is valid only in the local translation unit. In the C99 standard this definition is very vague, but in practice most compilers implement this in a similar sense to static inline. Essentially just inline overwrites any other function with the same name in any other linking unit. Thus if you declare a function as just inline in a header the compiler will expect to find a definition of it in the same compilation unit and will give you an error later if it doesn't.

Now if a function is to be both inlineable and available in other translation units then it needs to be defined as extern in the header declaration. Then the compiler won't look for it just in the current compilation unit.

static inline is by far the most portable definition at the moment and is constrained to the current translation unit. This is often found in headers together with the actual function definition.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...