Hello...
Currently I am using a header file that contains imported variables/functions/arrays at specific addressing. This is not ideal but it is the only workaround I have for my project. Now I'm trying to modularize my code therefore I am not able to include this header file in all modules as it will cause linker errors. I am using extern to get around that for var declarations. I was wondering if there is any way to import #define 's other than simply copying them over to each module. The following super rough code is to elaborate what I mean.
/* mem_import.h */ #ifndef MEM_IMPORT_H #define MEM_IMPORT_H volatile int flags _at_ 0; #define flags_array ((uint8_t xdata *)(0x0080)) #define operate_flags() (*(void (code *)(void))(0x00ff))() #endif /* main.c */ #include "module_a.h" #include "module_b.h" #include "mem_import.h" /* module_a.c */ // #include "mem_import.h" extern volatile int flags; /* module_b.c */ //#include "mem_import.h" extern volatile int flags;
I would like to be able to use the 'operate_flags()' and 'flag_array' macros in each module. Is there any way to do this that doesn't involve including the mem_imports.h or simply copying it over where ever it's needed. The reason I don't want to copy it over is I will loose the modularization because of the specific addressing.
Thanks for any help in advance and let me know if more clarification is needed.
What, exactly, are you "working around" ?!
"I am not able to include this header file in all modules as it will cause linker errors"
That's exactly why you should not have definitions in header files!
c-faq.com/.../decldef.html
"I will loose the modularization because of the specific addressing"
So why have it in a header file?
The definition (which includes the absoltue addressing) should be in a .c file; only the extern declaration should be in the header.
volatile int flags _at_ 0;
Goodness. If there ever was a position where you should not put an absolute-address object on a '51 CPU, that one's it. You've just usurped the memory location of R0.
Now I'm trying to modularize my code therefore I am not able to include this header file in all modules as it will cause linker errors. I am using extern to get around that for var declarations.
You got that bass-ackwards. The whole problem is that you're using extern in the wrong place.
I was wondering if there is any way to import #define 's other than simply copying them over to each module.
You already found that way: put them into #include files. That's what they're for.
Thanks Andrew. The problem was that the header is being created and exported from another project and it is certainly not the ideal, or honestly proper way of coding. I am aware of the difference between definitions and declarations, and where they should be located.
Sometimes you have to work with what you are dealt with. I have put in the request for the necessary changes... I was simply wondering if there is work around. Thanks for your time.
As I mentioned this was just rough code to make my point. This is in no way the actual code or the absolute addresses used.
I understand where externs should be used. The header mentioned is exported from another project so I cannot directly make changes to it. I was just trying to work around what I was dealt with, and after reading your comments I have made requests for changes in the other project. Thanks for your time.
Then whoever is responsible for that export should be shot!
As already noted, doing it in your own project is bad enough - but exporting it to anyone else's project is unforgivable!
... and function pointers have some serious issues in C51...
Thanks for the tip. I have heard about this issue and will keep an eye out for it. Just out of curiosity what is the cause of the issue?
Wresting then into the architecture :-)
Wresting then into the architecture
Come on smartie pants. How about some detail?
Take a closer look at the instruction set of the processor, and you would see that the processor does not have any great _general_ instructions for indurect addressing.
That also affects the ability to use a _real_ stack with indirect addressing of parameters and local variables relative to a base pointer. Which makes the C51 compiler convert parameters and local variables into reused global variables. And that optimization requirs the compiler/linker to know exactly what functions that can call other functions - how else would the compiler/linker know which two different local variables that can share the same global memory address?
The 8051 architecture is extremely hostile for use with high-level languages.
We agree with the posts warning you of the indirect addressing problems of the 8051. Look at the PK51 Linker Manual and pay close attention to the OVERLAY directives and their examples.
Bradford
www.keil.com/.../search.asp
Thanks for the info and link.
Cheers