Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 10, 2016, 09:45:56 09:45


Login with username, password and session length


Pages: [1]
Print
Author Topic: using arrays in both main.c file and other.c files for same project  (Read 1031 times)
0 Members and 1 Guest are viewing this topic.
bugmike25
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 15
-Receive: 13


« on: May 16, 2008, 02:54:42 14: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??/
Logged
zorx
Active Member
***
 Muted
Offline Offline

Posts: 121

Thank You
-Given: 48
-Receive: 163


« Reply #1 on: May 16, 2008, 03:58:17 15: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

Logged
bugmike25
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 15
-Receive: 13


« Reply #2 on: May 16, 2008, 07:11:18 19:11 »

Thanks it worked. Didn't know it was that simple.
Logged
YellowTiger
Guest
« Reply #3 on: May 19, 2008, 06:58:37 18: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
...
Logged
LabBoy
Guest
« Reply #4 on: May 23, 2008, 01:43:28 13: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];

.....
.....
.....




Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC