3

I'm using the checkpatch.pl script from the linux kernel for my own firmware since I use the same coding style (which I like).

There is just an error that I don't quite understand about global variables:

do not initialise globals to 0

For sure I want to avoid using globals as much as possible, but don't know why this is a style error?

Is it because some compilers don't put such globals in .BSS? (Usually they are smart enough)

alex
  • 158
  • 6

1 Answers1

2

First, it is redundant, and increases the size of the kernel (not what is finally loaded, but by having explicit instructions to the linker which are unnecessary).

It is part of a larger problem: Supposing that you had two different object files to link together, with different ideas of how to initialize them. Then the linker has to detect that and produce a symbol conflict error. The script is concerned with that as well.

Further reading:

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • About your second argument, wouldn't it be better to have an explicit error from the linker rather than believing that your global var is initialized to 0 while it is set to something else in another file? And anyway you are supposed to use the `extern` keyword for such cases. – alex Sep 10 '16 at 15:01
  • You're "supposed to", but some people say that it doesn't matter. – Thomas Dickey Sep 10 '16 at 15:06
  • My point is: if you don't use `extern` and don't initialize your global variable to 0 believing it is, this is even more confusing than an explicit error imo. – alex Sep 10 '16 at 15:18
  • http://stackoverflow.com/questions/16835716/bss-vs-common-what-goes-where#comment24275450_16836156 _Initializing something to 0 used to force the symbol into data on older gcc versions and was often used when you needed an possibility to edit the binary while keeping the initialized variable at 0. Most often (ab)used by kernels._ – alex Sep 10 '16 at 15:40