gary oblock
gary oblock

Reputation: 11

A library quesion regarding externs

This is regarding user code and user constructed libraries. If there is some global declaration:

bar_t fu;

Can fu possibly be accessed by a library? Note, I'm not concerned with normal programming practices, rather I'm concerned with the mere possibility.

Upvotes: 0

Views: 34

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 223843

If bar_t fu; appears outside of any function, then fu has external linkage.

Per the C standard, any other translation unit, including one in a library, can declare extern bar_t fu; to link to it, and then they can access the object named fu by using its name.

Linkers often have ways to strip names from object modules, so it may be possible to build the program in such a way that the library cannot link to the fu name, in spite of the C standard.

Of course, a library can also access an object if the address of the object is given to the library, or if the library abuses its access to memory to access things it should not.

Upvotes: 1

Related Questions