C99 refers to a built-in function: undefined reference to XXX and why should I put it in the header?

Is gcc compatible with the built-in model specified in the C99 standard?

I looked through some information about this issue. But I canโ€™t understand why the built-in function should be specified using "extern" or "static".

In a .c file, a call to an inline function defined in one translation module causes an error. What is the reason for compiler behavior?

I found the message Is โ€œbuilt-inโ€ without โ€œstaticโ€ or โ€œexternalโ€ ever useful in C99?

What does it mean?

If a call to a func function with an external link occurs where the visible built-in definition is visible, the behavior is the same as if the call was made with another function, say __func, with an internal link.

+4
source share
3 answers

The semantics introduced in C99 are a bit confusing, I must admit. The inline coefficient allows you to define alternative function definitions.

If a function is defined everywhere as inline both in declarations and in a definition, 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 way to static inline . Essentially, just inline overwrites any other function with the same name in any other binding node. Thus, if you declare the function as soon as inline in the header, the compiler will wait for its definition in one compilation unit and will later give you an error if this is not so.

Now, if a function should be both built-in and available in other translation units, it should be defined as extern in the header declaration. Then the compiler will not search for it only in the current compilation unit.

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

+8
source

inline function belongs to the header, so that all compilation units see the code.

If the code cannot be embedded for any reason, the final executable must have one version of the function with which it can be referenced. That you would have to "create an instance" in only one compilation unit (.c file) with something like

 extern inline void toto(int bla); 

which forces you to include a character in this unit.

You can find more about it here.

+4
source

You need to think about how the code is related. First, the source code is compiled into object files. These object files contain only the binary code of each function, which is determined by indexing the name of the function (or the changed name in C ++). Object files do not contain information about whether the function should be built-in or not. Therefore, this information should be placed in the header file, which is used in conjunction with the object file during the linking process. By using static before inline in your .c file, you make this function visible to any other function that uses this static inline PRIOR function for the compilation process. But once these functions are compiled into an object file, they cannot "embed" other functions from other object files.

So, simply by including the inline in the header definitions ( .h file) instead of the source file ( .c ), you can "embed" functions from one object file inside other functions from another object file during linking.

0
source

Source: https://habr.com/ru/post/1500716/