How does the compiler deal with built-in exported functions?

If the header file contains a function definition, it can be embedded by the compiler. If a function is exported, the name of the function and its implementation must also be available to clients during binding. How is this achieved by the compiler? Is it both a built-in function and an implementation for external subscribers?

Consider Foo.h:

class Foo { int bar() { return 1; } }; 

Foo :: bar may or may not be embedded in the foo.so library. If another piece of code includes Foo.h, it always creates its own copy of Foo :: bar, whether it is a string or not?

+4
source share
4 answers

Header files are simply copied to the source file - all #include . A function is only inline if declared using this keyword or defined inside a class definition, and inline is just a hint; this does not force the compiler to create other code or forbid you to do anything that you could otherwise do.

You can still take the address of the inline function or, which is the same thing, specify export it. For these purposes, the compiler simply treats it as non- inline and uses the β€œOne Definition Rule” (a rule that says that the user can’t "apply two definitions to the same function, class, etc.) to" ensure ", a function is defined once and only one copy is exported. Usually you are allowed to have only one definition among all sources; an inline function must have one definition that is repeated exactly in every source that it uses.

Here is what the standard has to say about inline extern (7.1.2 / 4) functions:

The built-in function must be defined in each translation unit in which it is used and must have the same definition in each case (3.2). [Note: a call to a built-in function may be encountered before its definition appears in the translation block. ] If a function with external communication is declared to be built-in in one translation block, it must be all translation units in which it appears; no diagnostics required. a built-in function with external communication must have the same address in all translation units. A static local variable in an external inline function always refers to the same object. A string literal in an external inline function is the same object in different translation units.

+2
source

This usually means that it creates a separate built-in method for each obj file that uses it during the link. It may also fail or abandon the built-in functions, so this can cause problems because you can complete bloated objects without benefiting from the attachment. The same thing can happen with embedding a virtual method, so it can be forced to inining and setting a warning for a built-in failure (the only useful warning message compiler).

+1
source

When exporting, I assume that you mean something like getting a pointer to a function and then calling the function through a pointer.

Yes, in this case, the compiler will generate a regular function so that it can be called from a pointer.

One way to do this is with a link once. The idea is that in the translation block it receives the code in a special type of section, which has a name based on the name of the function. During linking, the linker will contain only one instance of the same named sections once.

0
source

built-in functions do not exist in the compiled binary: this is because they are taken and placed directly on the call site (the so-called IN-LINE). Each use of the built-in function leads to a complete code that needs to be inserted into this place.

Therefore, inline functions cannot be exported because they do not exist. But you can use them if you have a definition in one title. And yes, you MUST provide a definition for the inline function, otherwise you will not be able to use it.

If you manage to export the built-in function, then it is sure that it is no longer built-in: inline is not a strict semantic element. Depending on the parameters of the compiler and the compiler, one compiler may choose the built-in, another not, sometimes provide a warning, and sometimes even an error (which I usually prefer to be the default behavior, since it shows the places where unintended situations occur)

-one
source

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


All Articles