Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By MartinC
#73904 OK this took me a little while to figure out so I thought I'd share it in case anyone else encounters the same problem.

Since v0.9.5 of the SDK there has been a really useful way to save RAM by automatically putting all static strings from os_printf() statements into Flash. You do it by defining 'USE_OPTIMISE_PRINTF' (e.g. with a -D in your Makefile), which enables a macro to put them all in the .irom.text linker section (see note 4 under 'optimize' in the v0.95 release notes).

So far so good but when I combined this with putting some of my own stuff in .irom.text, I started getting 'section conflict' errors from the compiler.

Turns out the problem is because since GCC3.0 you can't mix const and non-const data in the same linker section (see here). The strings created by USE_OPTIMIZE_PRINTF are tagged const, so if you want to put your own data in irom0.text as well, then you must make all of it const too. This is sensible anyway since you're putting it in flash.

However there is one final 'gotcha' to be aware of: if you want to put a string array in flash, then you must ensure that its type is correctly declared as a constant pointer to constant data - for example:
Code: Select allconst char string1[] __attribute__ ((section(".irom.text"))) = "foo";
const char string2[] __attribute__ ((section(".irom.text"))) = "baz";

const char * const strArray[] __attribute__ ((section(".irom.text"), aligned(4))) = {
    string1,
    string2 };

Note in particular the second const in the definition of strArray[]. Note also the aligned(4) attribute which ensures that the pointers are stored on a 32bit boundary.