Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22298

C: private and public - visibility of variables [closed]

$
0
0

Intro:
Most Object Oriented programming languages provide keywords that modify the visibility/access to single or multiple variables of an object. E.g.: the public, private or protected keywords in Java or as access specifiers in C++.
Now the concept of classes and objects does not exist in C, but that doesn't mean, that changing the visibility of a variable is useless. Infact, it is desired to change the visibility.

In C the only way to manipulate the accessability/the visibility of a variable to the global context is static, which has the downside of changing the segment too. (EDIT: Changing the segment to .bss means, that the initialization cannot be done at compile time: code at runtime has to initialize these variables now.)

Ideas:

  • Make the compiler (gcc) compile all variables (that are not ro) into the same segment, but it seems like there is not such compiler option. (The Visual C Compiler provides such an option: /merge)
  • gcc provides a visibility option, an option that allows someone to change the visibility of a variable. -fvisibility=[default|internal|hidden|protected]. Even though that option is listed within the list of options for Code Generation Conventions, it
    1. specificly states, that it "Set[s] the default ELF image symbol visibility to the specified option" - GNU-Docs, which means, that the compiler output is not affected, rather the linked executable/library is and
    2. the compiler output is not affected, which means, that a compiled unit/file with that option does not hold the information of removing unwanted symbols -> it seems you have to specify that option again while linking.
  • gcc provides an attribute feature: __attribute__ ((visibility ("hidden"))), but there propably is no gcc version that actually implements this attribute. (if there was a difference to the -fvisibility compiler option, a preprocessor macro would do the job)

It is a waste of time to compile every singular source file into a static library (which would actually be affected by -fvisbility); it would be much easier to compile a library by specifing all of the source files.

How could one implement the feature of visibility of a variable in C(as described above), while also not having to modify a C compiler and not adding additional compilation steps (like manually sieving through the binary output)?
Maybe a compiler option that automatically adds the static keyword to every variable that is not set as auto AND another compiler option that merges the .bss segment with the .data segment?

Comment:One context of variable visibility (the global context) can be quite annoying on larger projects.

EDIT: (clarification)
Even though the concept of private in the sense I described is based on what the linker does with the compiled source files, the compiler has to somehow hide the variables marked as private (in the C code) in a way, that makes the linker unable to link against them.


Viewing all articles
Browse latest Browse all 22298

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>