How does C limit the use of a static function to just its file?

I understand that a static function in C allows this particular function to be called only within this file. I am interested in how this happens. It is placed in a specific part of the memory or the compiler applies a specific operation to this function. Can the same process be applied to calling a function in an assembly?

+8
c assembly
source share
4 answers

It does not fall into the table of object names, which prevents it from being associated with other material.

+11
source share

Declaring a static function does not really prevent it from being called from other translation units.

What static means is that it prevents the transfer of a function (related) from other translation units by name. This will exclude the possibility of direct calls to this function, that is, it will call "by name". To do this, the compiler simply excludes the function name from the table of external names exported from the translation unit. In addition, there is nothing special about static functions.

You can still call this function from other translation units in other ways. For example, if you somehow got a pointer to a static function in another translation unit, you can call it through this pointer.

+12
source share

Functions and other names are exported as characters in the object file. The component uses these characters to resolve all kinds of freezing links during a connection (for example, calling a function defined in another file). When you declare it static , it just will not be exported as a symbol. Therefore, it will not be picked up by any other file. You can still call it from another file if you have a pointer to it.

+6
source share

This is actually the opposite. When a function is not static, its name is written somewhere in the object file, which the linker can then use to link other object files with this function to the address of this function.

When a function is declared static, the compiler simply does not put the name there.

+2
source share

All Articles