PROGMEM Problem

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I've a problem using the Progmem stuff:

The following code works fine:


#include 
#include 
#include 
const unsigned char SKB[] PROGMEM = {0,1,2,3};

void main(void)
{
...
}

But if I want to move the "const un...." construct towards a header file, the compiler fails permanently (Error:
Linking: C1210110.elf
Security.o(.progmem.data+0x0): multiple definition of `SKB'
C1210110.o(.progmem.data+0x0):C1210110.c:81: first defined here

But, there's nothing defined in the line mentioned (I've deleted the line in the main file!)
Also make clean doesn't make it better.
Any ideas?
Andreas

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What I do is have a separate .C file for "global" static const data called global.c in which I'd include that line you give. I also have a global.h that is included in any C files that want to access the data and in that I'd have something like:

extern const unsigned char SKB[] PROGMEM;

Then two or more different .c files can each access the single SKB[] which I guess is what you are trying to achive. BTW, the same applies equally to global SRAM variables as it does to const data in PROGMEM.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you include a header file that includes global variable declarations in more than one "C" file, then each "C" file will have its own copy of the of the variable. Unless a module has a "static" tag attached to its global variable declarations, the variables will be global in scope across every module that gets linked to produce the final program.

(Unfortuntely, the variable's slot in the global namespace may already be occupied, even though other modules may not know anything about its prior existence. This is where the "extern" declaration becomes useful. "extern" tells the compiler to assume that a particular variable exists, but that it shouldn't take any steps to actually create an instance of that variable; rather it should count on the fact that an instance of the variable already exists in some other module.)

When the time comes to link the modules together, the linker will be confused because it sees multiple variables with the same name, all of whom are trying to take the same place in the global namespace.

(If you modified the header to include a "static" attribute on the variable declaration, then things would compile and link just fine, but a separate copy of the variable would be declared for each module which includes the header. This is usually *not* what you really want to do.)

This leads us to Cliff's suggestion. Only ever declare the variable in one specific C module. Then, create a header file with an "extern" reference to the variable. Include that header in every other C module that you want to have access to the variable. Now, there will only be one instance of the variable declared, and every other module will "look at" that single instance.

- Luke