Order of definitions in GCC

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

Hi gurus,

I've struggled, searched the forums, read the docs, and still can't answer this one. So if anyone has any ideas...

I am putting some structures in Flash, and these structures include pointers to strings (which are also in Flash). If I use this (simple but working example) code:

// First we define the structure data type
struct MenuStruct {
PGM_P MenuName; // pointer to display name (a string)
};

// Here are the structures themselves, one for each menu item
const struct MenuStruct Menu1Struct PROGMEM = { Menu1Name };

// Here are the menu display name strings
const char Menu1Name[] PROGMEM = "Menu Item 1";

It fails compilation on the const struct ... line, saying that Menu1Name is not defined. If I move the Menu1Name string up, so it's above the structure, the compiler is happy.

How do I fix this without moving the string????

Later in my code I have a linked-list of structures, which in the nature of linked-lists point to later structures. There I get this same error, and I can't just move things around to make it go away. I'm suprised that the compiler cannot accept a reference to a "future item" in this example. I'm obviously missing something here. Can anyone shed any light?

Thanks very much.

Frank.

http://www.frankvh.com/mp3player

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

I don't think it is possible. You are trying to initialize struct with undefined yet variable. That's make compiler unhappy.
Why don't you go with:
const struct MenuStruct Menu1Struct PROGMEM = { "Menu Item 1"};?
I guess you need to have var Menu1Name[] PROGMEM, then you can try
#define MenuItem1 "Menu Item 1"
And use this macro in both instances. Optimization should use the same pointer in both cases.

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

That's where I really have a problem. Because I want to make a linked-list of structures. A stucture which contains a pointer to the next structure, which contains a pointer to the next structure, etc etc. The usual linked-list deal. So how is a linked-list possible if you cannot reference forwards?

I thought the purpose of the linker was to resolve these unresolved addresses. Similar to how you can call a function which is defined further on in your .c file. In that case the address of the function is not yet known by the compiler; that's the linker's task.

Which is what's making me puzzled - how is this any different?

Thanks as always,

Frank.

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

Linked lists are not normally used that way, they are normally only
used to store dynamic data where the actual amount of data is only
known at run-time.

I didn't look too closely at your case, but wouldn't it be more
feasible to use a compile-time defined fixed array instead? Unlike
the linked list, you can even reference that directly.

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

That is not a case! You can define your structures any way you like, but how you can possible intilize it?! You can only declare first - initialize second or initialize later in a run time mode.

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

Thanks Bill. I tried that, and it didn't work. Well, the compiler didn't complain directly about the forward structure declarations, but it didn't use them either. My forward references contained in the linked-list contents still resulted in "not defined" compiler errors.

So... I gave up on using structures, and instead created some arrays of pointers instead. All of my structure elements were pointers anyway. I created arrays of:

PGM_VOID_P array1[] PROGMEM = { ..., (PGM_VOID_P)&array2[0], ... };
PGM_VOID_P array2[] PROGMEM = { ... };

etc, where the array contents are pointers which are all cast to (PGM_VOID_P). The array contents point to the other arrays, as linked lists do. I also did Bill's forward declarations beforehand, such as:

PGM_VOID_P array2[];

This worked! So, it appears that forward declarations for structures in flash don't work, but forward declarations for arrays in flash do work. I now have my linked-list in flash.

Thanks to those who replied, and I hope this solution helps someone else in the future.

Frank.

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

Hi all,

Just for posterity, the above solution did NOT work. GCC put the first array in flash and all subsequent arrays in SRAM. Despite them all being identically defined with a PROGMEM. This became clear when I viewed the generated listing file.

The solution I ultimately used was to define arrays of structures. This did work, and is now in use in my MP3 player. If you want to see the source, it's posted on my website at: http://www.frankvh.com/mp3player.

Frank.

MP3 Player Home