It's all in how you set up the linkage. For a binary that loads into an operating system, you want the initialized segment to load right into memory along with the code.Sik wrote:It's an issue with all compilers. They expect the values to be initialized before calling main().
For ROM code, you need two things:
1) a section which takes the values of the initialized segment but goes into ROM. Note that it should be referenced in the code by the RAM address, which will be different from the ROM address. With GCC, this is done by properly setting up the linker file. (don't ask me how, I won't know until I figure out how to do it, and I'm not trying right now)
2) a routine called at startup (before main is called) to copy the initialized segment into RAM
Or you could just forget about initialized globals and write your code not to use them. Except that then you'll have a problem with pre-initialized arrays, which are very nice to have. I think you can declare them "const" and they'll go into a read-only segment (which means ROM), but of course you have to remember to use "const" every time.