avr-gcc: variable must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’

StackOverflow https://stackoverflow.com/questions/21217429

  •  29-09-2022
  •  | 
  •  

Question

I am trying to reproduce this 4-key-keyboard and for that I am trying to compile its source by compiling it with avr-gcc on my Linux box.

I managed to solve a couple errors thrown by the compiler by extending the command line with paramaters, but now I am stuck with the errors below. Thing is that quite a few demo projects on V-USB use the same libraries and throw the same errors and I don't want to wrestle through all the code to try and fix them for every project I want to check out. I realize the best way to go is to fix the errors in the source code, but although the errors below can trivially be solved, new errors are thrown that are much more complicated to solve and with all changes to the source code I have no guarantee that the resulting program will still actually work.

My question is: Does avr-gcc have some compatibility command line parameter that makes the code compile as if it was an older version of gcc?

This is the command I use to compile the sources:

avr-gcc main.c -I /usr/lib/avr/include/ -mmcu=attiny85 -DF_CPU=16000000 -Os -I ./usbdrv

These are the errors thrown by avr-gcc:

In file included from main.c:32:0:
./usbdrv/usbdrv.h:455:6: error: variable ‘usbDescriptorDevice’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
./usbdrv/usbdrv.h:461:6: error: variable ‘usbDescriptorConfiguration’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
./usbdrv/usbdrv.h:467:6: error: variable ‘usbDescriptorHidReport’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
./usbdrv/usbdrv.h:473:6: error: variable ‘usbDescriptorString0’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
./usbdrv/usbdrv.h:479:5: error: variable ‘usbDescriptorStringVendor’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
./usbdrv/usbdrv.h:485:5: error: variable ‘usbDescriptorStringDevice’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
./usbdrv/usbdrv.h:491:5: error: variable ‘usbDescriptorStringSerialNumber’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
main.c:105:14: error: variable ‘usbDescriptorHidReport’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’

My configuration:

Ubuntu 13.10

avr-gcc (GCC) 4.7.2

Was it helpful?

Solution 2

The usbdrv/ directory needs to be updated to the latest version. Download the V-USB tarball or zip file and replace the project's usbdrv/ with the one from the archive. You may need to massage the project's usbconfig.h a bit in order to make the new V-USB work with the existing project, although most of the time it should just work.

OTHER TIPS

I know this post is a bit hold. But this may help someone.

The reason for that error is that new avr-gcc needs PROGMEM to be const. You just need to put const in front of every line that begins with PROGMEM .

Sed fixes this nicely with sed -i 's/^PROGMEM/const PROGMEM/g' usbdrv/*.

You have not given the code where the error comes out but by just seeing the error message, the problem is very clear

./usbdrv/usbdrv.h:455:6: error: variable ‘usbDescriptorDevice’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’

you can only put constants in the read-only section (flash for example). All variables go to RAM. Therefore if you want that "usbDescriptorDevice" (which I dont know what it does) goes in the read only section (because you are putting "progrmem") you should declare that as constant.

The problem usually appears with old libraries that don't comply with this simple rule.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top