简体   繁体   English

avr-gcc在PROGMEM中未看到“ const”修饰符

[英]avr-gcc not seeing “const” modifier with PROGMEM

I have a file like this: 我有一个像这样的文件:

#include <avr/io.h>
#include <avr/pgmspace.h>

const PROGMEM char* str = "Hello UART!\r\n";

I'm trying to compile it with a Makefile, this is the final command: 我正在尝试使用Makefile进行编译,这是最终命令:

avr-gcc -std=gnu99 -mmcu=atmega328p -DF_CPU=16000000UL -I. -Ilib/
    -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
    -Wall -Wno-main -Wno-strict-prototypes -Wno-comment -g2 -Wextra
    -Wfatal-errors -Wno-unused-but-set-variable -ffunction-sections
    -fdata-sections -Wl,--gc-sections -Wl,--relax -Os main.c lib/uart.c
    --output main.elf

I am getting the following error: 我收到以下错误:

main.c:9:21: error: variable 'str' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
 const PROGMEM char* str = "Hello UART!\r\n";
                     ^
compilation terminated due to -Wfatal-errors.
Makefile:71: recipe for target 'main.elf' failed
make: *** [main.elf] Error 1

What's wrong with my code? 我的代码有什么问题?

I tried moving the PROGMEM keyword in various places of the declaration, without any change. 我尝试在声明的各个位置移动PROGMEM关键字,而没有进行任何更改。

const char str[] PROGMEM = "Hello UART!\r\n";

should work. 应该管用。 You're creating a non-const pointer to const data. 您正在创建指向const数据的非const指针。

Your code declares a non- const pointer to a(n array of) const char . 您的代码声明了一个指向const char (n个数组)的非const指针。 That is, the compiler is exactly right that variable str is non- const , it is the thing to which it points that is declared const . 也就是说,编译器完全正确,变量str是非const ,声明为const就是它指向的东西。 If you want both pointer and pointed-to array to be const , then that would be this: 如果您希望指针和指向数组都为const ,那就是这样:

const PROGMEM char* const str = "Hello UART!\r\n";

or equivalently, this: 或等效地,这:

PROGMEM char const * const str = "Hello UART!\r\n";

Those two mean exactly the same thing as each other. 这两个意思彼此完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM