Global constants without using #define

Well, I want to define a set of memory addresses as constants in the .h file, which is used by a bunch of .c files (we are in C, not C ++). I want to be able to see the variable name instead of just seeing the hexagonal address in the debugger ... so I want to convert the #defines that I currently have into constants that are global in volume. The problem is that if I define them as follows:

const short int SOME_ADDRESS = 0x0010 

then I get a terrible "multiple ads" error, since I have several .c files using the same .h. I would like to use an enumeration, but this will not work, since by default it enters an integer (this is 16 bits in my system ... and I need to have finer type control).

I was thinking about putting all the addresses in the structure ... but I have no way (I know) to set the default values ​​of the structure instance in the header file (I do not want to assume that the particular .c file first uses the structure and fills it in another place.I would really like to have the constants defined in the .h file)

It seemed so simple when I started, but I don’t see a good way to define a globally accessible short constant int in the header file ... does anyone know a way to do this?

thanks!

+7
source share
2 answers

Declare constants in the header file using extern :

 extern const short int SOME_ADDRESS; 

then in any, but only in one .c file, a definition is provided:

 const short int SOME_ADDRESS = 0x0010; 
+23
source

If you are compiling with gcc, you can add the -ggdb3 switch in which gcc will store macro information (i.e. #define s) so that they can be used inside gdb.

0
source

All Articles