Restrict Global Variables to specific modules

Here comes a very special wish - please do not call me crazy:

Working with many C modules, I prefer to define protected/"dangerous" variables static to one module. Quite often I would like to have a variable global only in a restricted number of modules - typically only in one module and then further the the communication modules (e. g. "comm1.c", "comm2.c").

To achieve this, it would be terrifically nice, if I could create the following code with a special pragma modifier (I made the examples with two modules "comm1.c" and "comm2.c", to keep the examples more general - but in fact I would be perfectly happy, if this would work with restriction to one further module "comm.c"):

#pragma RestrictedGlobal "comm1.c"
#pragma RestrictedGlobal "comm2.c"
int iRestricted;

(alternatively also like this (but then it would not work in other C compilers):

#pragma allowAccess "comm1.c"
#pragma allowAccess "comm2.c"
static int iRestricted;


)

In "comm1.c", "comm2.c" then the extern command

extern int iRestricted;


should be allowed - but it any other module it should fail (best would be, if the command itself would be allowed, but if the access to iRestricted would fail in any other module - then it is possible to include the extern declaration in some header file without the danger of failure)

I know that this is not possible in any other C compiler, but I think pragma is not standardized, so C compiler producers can add their own - just I think better use the first of the above alternatives, as then the code will also work in other C compilers not recognizing this pragma.

... I know that this might be quite an effort, as it concerns the very interior of the compiling/linking tables ... so just a polite question ... .

Parents
  • please do not call me crazy:

    All right --- I'll just call you misguided then.

    I know that this is not possible in any other C compiler,

    Forget about C compilers --- this would be in clear and brutal violation of the principles of C itself. So if you really think you need stuff like that, maybe you should reconsider your choice of programming language. Using C++, with namespaces and friend declarations, what you want just might be possible.

    But at the heart of it, this wish is misguided. If you don't have the discipline to keep yourself and co-workers from violating rules such as "you do not #include other module's private headers" or "never use 'extern' in *.c files", then you'll have much bigger problems than this one anyway ... so even if some compiler had this feature, that wouldn't solve the actual structural problem with the way your team develops code.

Reply
  • please do not call me crazy:

    All right --- I'll just call you misguided then.

    I know that this is not possible in any other C compiler,

    Forget about C compilers --- this would be in clear and brutal violation of the principles of C itself. So if you really think you need stuff like that, maybe you should reconsider your choice of programming language. Using C++, with namespaces and friend declarations, what you want just might be possible.

    But at the heart of it, this wish is misguided. If you don't have the discipline to keep yourself and co-workers from violating rules such as "you do not #include other module's private headers" or "never use 'extern' in *.c files", then you'll have much bigger problems than this one anyway ... so even if some compiler had this feature, that wouldn't solve the actual structural problem with the way your team develops code.

Children
More questions in this forum