View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
Is possible to encapsulate variables within a file? |
Posted: Fri Nov 12, 2021 2:38 pm |
|
|
I have a big project that I'm working on and now I need to add an old module (.h file) to add WiFi support and many variables have the same name of the current main project.
So, there's any way to encapsulate the variables of the new .h file so they are not duplicated and avoid to rename all of them?
CCS 5.091
PIC18F67J50 _________________ Electric Blue |
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Fri Nov 12, 2021 2:49 pm |
|
|
Just a suggestion.
Use a struct to all your global var. Maybe mamed wifi... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
Re: Is possible to encapsulate variables within a file? |
Posted: Fri Nov 12, 2021 4:00 pm |
|
|
E_Blue wrote: | I have a big project that I'm working on and now I need to add an old module (.h file) to add WiFi support and many variables have the same name of the current main project.
So, there's any way to encapsulate the variables of the new .h file so they are not duplicated and avoid to rename all of them?
CCS 5.091
PIC18F67J50 |
you can set the variables as static (so they don't generate symbol table symbols) and then use #module above the variable definitions which forces the compiler to not allow other files to use what is below #module (so you'll need function declarations above the #module for any functions you want to use outside the file). I believe they can have the same name at that point provided the H file they are in cannot see the other variables of the same name in other files (so you have to be weary of the order of #includes...also don't ever #include after a #module in a file).
Though honestly I just name the variables with a prefix when making modules like wifi_variablename for the wifi file. Just makes my life easier. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Fri Nov 12, 2021 4:09 pm |
|
|
As an example of the module approach:
module_1.h
Code: |
#ifndef MODULE_1_H
#define MODULE_1_H
unsigned int m1_getvar();
#module
static int variable = 1;
unsigned int m1_getvar(){ return variable; }
#endif
|
module_2.h
Code: |
#ifndef MODULE_2_H
#define MODULE_2_H
unsigned int m2_getvar();
#module
static int variable = 1;
unsigned int m2_getvar(){ return variable; }
#endif
|
main.c
Code: |
#include <24fj64ga004.h>
#include "module_1.h"
#include "module_2.h"
void main() {
unsigned int v1 = m1_getvar();
unsigned int v2 = m2_getvar();
while(TRUE);
}
|
you can see they create two separate file scope variables of the same name with different locations in the SYM file:
Code: |
W0 @SCRATCH
W0L _RETURN_
W0 -W1 @READ_ROM_MEMORY.P1
W0 -W1 @DIV3232B.P2
W1 @SCRATCH
W1 @READ_PACKED_MEMORY.P1
W1 @WRITE_PACKED_MEMORY.P2
W2 @READ_ROM_MEMORY.P1
W2 @WRITE_PACKED_MEMORY.P2
W2 -W3 @DIV3232B.P3
W2 @READ_PACKED_MEMORY.P4
W3 @WRITE_PACKED_MEMORY.P1
W3 @READ_PACKED_MEMORY.P2
W3 @READ_ROM_MEMORY.P3
630.6 C1OUT
630.7 C2OUT
800-801 variable
802-803 variable
804-805 MAIN.v1
806-807 MAIN.v2
2780-27FF _STACK_
|
|
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Fri Nov 12, 2021 4:39 pm |
|
|
Thanks for your time.
I started to rename all the variables in the WiFi module but then I realized that the main old program have some routines that the new main program has.
To be more clear.
The old WiFi firmware has two modules, the main that calls the WiFi modules and handles the user interaction and the WiFi module itself.
The new program, currently do the same but with a GPRS modem and is all in the same .c file except for some EEPROM address definitions and default values.
Now I need to add WiFi support to this new project so I want that the compiler only search for variables in the .h file only, when is compiling that file; so I will get an error when a common variable, between the old and the new main, is used by the WiFi module.
After that I can go variable by variable adding them as I needed or share them if is needed. _________________ Electric Blue |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
|
Posted: Tue Dec 07, 2021 11:41 am |
|
|
When building projects using #include to pull in other C files, rather than using multi-unit compilation, would static even do anything? Wouldn’t they been seen to every line of code that was included, as if it was one huge C file? _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Tue Dec 07, 2021 2:01 pm |
|
|
allenhuffman wrote: | When building projects using #include to pull in other C files, rather than using multi-unit compilation, would static even do anything? Wouldn’t they been seen to every line of code that was included, as if it was one huge C file? |
If you are using #module, other files cannot see below it, even if you #include. Additionally static is supposed to prevent other files from using extern declarations on your variables (you can use extern even if not using a linker). I haven't tested to see if CCS conforms to this or not.
Static makes the variable or function "file scope" removing it from the external symbol tables that compilers generate (which exist even if not using a linker). Though again, I don't know how CCS conforms to this. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
|
Posted: Tue Dec 07, 2021 2:23 pm |
|
|
jeremiah wrote: |
If you are using #module, other files cannot see below it, even if you #include. Additionally static is supposed to prevent other files from using extern declarations on your variables (you can use extern even if not using a linker). I haven't tested to see if CCS conforms to this or not. |
I believe early on I found that static variables were still accessible anywhere in my program, and that might have even been using multi-unit compilation. I’ll have to look at my old test program.
I will read up on #module. That’s a new one to me. Even now that I have switched out new projects to #include (due to everyone here always telling me to stop using multi units ;), I still use static and other things to make the code more portable if it goes to a different environment. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
|