External linkage of objects - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.8 Declarations and Definitions (https://forum.misra.org.uk/forumdisplay.php?fid=35) +---- Thread: External linkage of objects (/showthread.php?tid=437) |
External linkage of objects - Lundin - 07-01-2008 I'm sitting here and writing a code standard for our company which includes MISRA-C. Among the first things I did was to ban external linkage of objects (ie variables) entirely, as it is one of the most common causes of \"spaghetti-code\". After doing this, I found myself having an easy time to make my rules conform to the whole chapter \"Declarations and Definitions\". Plenty of the issues addressed won't be any issues at all, if external linkage of objects is forbidden. (External linkage of functions will naturally be allowed, since the prototypes should be in the h-file and the definitions in the c-file.) If external linkage of objects was forbidden, it would smoothen plenty of the rules, as they would then only apply to functions and not objects. 8.4 and 8.12 could probably be removed entirely. So I am curious why MISRA didn't forbid it? No matter how hard I try, I can't come up with a situation where external linkage of objects is motivated. Since the proper way to pass variables between files in C is like this: Code: /* module.h */ Code: /* module.c */ Code: /* main.c */ This enables the only part of object orientation that ISO C supports, namely private variable encapsulation. The only argument I can think of against the above method is efficiency. But that is not a good argument, as most compilers support inlining of functions, which will make the above code as efficient as it gets, with direct memory access to the variable. And since that can be done, even hardware registers can be declared this way. Perhaps there is some case I didn't think of? I would be most curious to know whether MISRA has had this in mind, or perhaps if external linkage was allowed \"to avoid programmer shrieks\", as it is a commonly used syntax? - George Brown - 09-01-2008 Hi Lundin, If you are able to enforce the banning of external objects, then do so! But in general, most programs written in C do use external objects. AUTOSAR allows the direct reading and writing of external objects across the RTE (Run-Time Enviroment). The abstraction of using read_x and write_x handles between software components was considered too excessive. Shame - Given the abstraction, it could have been implemented to degenerate into direct access with no overhead. What do other people think? Would you like to see a MISRA rule banning external objects? George Re: External linkage of objects - william.forbes - 09-04-2008 Yes. But I think lots of people will complain! There is a lot of legacy code that people have to use and hence this may be extreemly difficult to achieve in practice. Bill Forbes Re: External linkage of objects - misra-c - 21-05-2008 It is interesting to note that avoiding external objects achieves compliance with the "Declarations and Definitions" rules. However, there are other methods of achieving compliance and these have the advantage, as noted by william.forbes, of being compatible with legacy code. For this reason, a MISRA rule banning external objects is not currently planned. |