External built-in functions must have the same address in all translation units. How is this achieved?

In accordance with the standard, external built-in functions must have the same address in all translation units.

How is this achieved in the compiler? I mean, when I form part of a translation unit, I have no idea what another TU will look like. So how can I have the same address everywhere?

+6
source share
2 answers

This is before implementation, but this is usually resolved by the linker. Each compiled translation unit will create an object file containing a copy of the function, marked in some way, so that the linker knows that it should expect (and accept) duplicates. The component will include one of them, discard the rest and allow any links to the function.

+6
source

A simple strategy: every time such an inline function is defined, compile it for the object time, as if it were a normal function. Then, during the connection, detect duplicate functions and delete them, leaving one copy of each. This is how C ++ compilers worked (also in the face of templates) about 10 years ago. Not sure how they do it these days.

+4
source

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


All Articles