Title: using arrays in both main.c file and other.c files for same project Post by: bugmike25 on May 16, 2008, 01:54:42 13:54 Hi all. I'm having a problem with compiling my program because i want to use the same array inboth my main c file and my other c file.
Example: main.c #include <absacc.h> #include <stdio.h> #include <SPI.h> #include <DS1305.h> #include <SDcard.h> unsigned char rdat[12]; //To be used in main.c and SDcard.c However if i declare it in both i get an error: Multiple public definitions and if i declare it in only 1 of the two the compiler thinks that the array is undefined in one of them. Please help me out. Can i solve this problem by defining the array in the SDcard.h file instead or any other solutions??/ Title: Re: using arrays in both main.c file and other.c files for same project Post by: zorx on May 16, 2008, 02:58:17 14:58 You have to use extern , like this:
main.c ------- #include <absacc.h> #include <stdio.h> #include <SPI.h> #include <DS1305.h> #include <SDcard.h> unsigned char rdat[12]; . . ---------------------- SDcard.c ----------- . . extern unsigned char rdat[12] . . . --------------------------- In SDcard.c this means that rdat[12] is externaly defined (in this case in main.c) and can be used across files. Hope that this works. Regards Title: Re: using arrays in both main.c file and other.c files for same project Post by: bugmike25 on May 16, 2008, 06:11:18 18:11 Thanks it worked. Didn't know it was that simple.
Title: Re: using arrays in both main.c file and other.c files for same project Post by: YellowTiger on May 19, 2008, 05:58:37 17:58 You have to use extern , like this: extern unsigned char rdat[12] There's the way to declare all the related vars just once: "MySDLib.h" ------------------------- // here follows block of SD-related variables declarations: #ifndef _SD_Card_Lib_ #define _SD_DefArea_ extern #endif _SD_DefArea_ int iVar1; // all SD-related variables are declared just once, all in one place - this will ease the control over the code and variables _SD_DefArea_ char cVar2; // ... _SD_DefArea_ unsigned char rdat[12]; // ... #undef _SD_DefArea_ ... "SDLib.c" ------------------------- #define _SD_Card_Lib_ // this will turn extern OFF #include <MySDLib.h> // all SD-vars will be declared as usual // SDLib functions follows... ... "main.c" ------------------------- // #define _SD_Card_Lib_ // omitting this #define will turn extern ON #include <MySDLib.h> // all SD-vars will be declared as external ... Title: Re: using arrays in both main.c file and other.c files for same project Post by: LabBoy on May 23, 2008, 12:43:28 12:43 Hi
yor can share global variable(s) in other files by using "extern" for example //-------[file A.C]-------- char arr[10]; ..... ..... ..... //-------[file B.C]-------- // if you want to access arr in file A.C, you can declar in the top of file B.C extern char arr[10]; ..... ..... ..... |