Go to Post Remember, FIRST is merely a means to an end - the end result being that we completely change the world for the better. - RogerR [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-02-2006, 22:07
Smithvillefirst Smithvillefirst is offline
Anthony Peck
FRC #1806 (Smithville First)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Smithville
Posts: 9
Smithvillefirst is an unknown quantity at this point
Using vars across multiple .c files

We have a few global variables that we want to use in both user_routines.c and tracking.c. We can not seem to declare and access them in a way that the compiler likes. We either get an error saying multiple definitions, or not defined. I've tried defining the var in a globals.h and including that in both .c files, but that doesn't work. I've tried adding extern declarations in my .c files, but no dice there either. I've tried about every combo of declaring in .c and .h files, with and without externs but nothing seems to work. Anybody know what my problem is?
__________________
Anthony Peck
Integration Manager
Team 1806
  #2   Spotlight this post!  
Unread 01-02-2006, 22:10
Jared Russell's Avatar
Jared Russell Jared Russell is online now
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,070
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Using vars across multiple .c files

define the var as:

extern mytype myvar;

in each file accessing it, except one.

Then, in ONLY ONE file (the only without an extern declaration), declare the variable normally:

mytype myvar;

See http://www.crasseux.com/books/ctutor...variables.html

Last edited by Jared Russell : 01-02-2006 at 22:17.
  #3   Spotlight this post!  
Unread 02-02-2006, 10:51
Smithvillefirst Smithvillefirst is offline
Anthony Peck
FRC #1806 (Smithville First)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Smithville
Posts: 9
Smithvillefirst is an unknown quantity at this point
Re: Using vars across multiple .c files

I've tried this, but it won't work. In a file globals.h, I have the following:
int myvar;

Then, in user_routines.c I have:
#include "globals.h"
extern int myvar;

I do the same thing in tracking.c, with the include and extern declaration but I get the following linker error when I try to compile:
symbol 'myvar' has multiple definitions

I access the variable inside the default_routine function, and in another function in the tracking.c file. I've tried declaring it both inside the functions, and at the top of the .c file outside of any function, but below my preprocessor directives. Nothing seems to work. Everything look fine, but what am I doing wrong? Thanks for any ideas you guys might have.

Quote:
Originally Posted by Abwehr
define the var as:

extern mytype myvar;

in each file accessing it, except one.

Then, in ONLY ONE file (the only without an extern declaration), declare the variable normally:

mytype myvar;

See http://www.crasseux.com/books/ctutor...variables.html
__________________
Anthony Peck
Integration Manager
Team 1806
  #4   Spotlight this post!  
Unread 02-02-2006, 11:00
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: Using vars across multiple .c files

Never declare variables in a header file. You can extern the variables, but never declare them. If you declare the variable in the header file, the compiler tries to create the variable each time it reads the header file.

Here's an example of what you should do:
Code:
foo.c:
int myvar;

void foo(void)
{
  myvar = 2;
}


bar.c:
extern int myvar;

void bar(void)
{
  printf("myvar %d\r", myvar);
}
Alternatively you could get rid of the 'extern int myvar'; from bar.c and move it to a header file that you include in any c file that uses myvar.
Code:
foo.c:
#include "globals.h"
int myvar;

void foo(void)
{
  myvar = 2;
}


bar.c:
#include "globals.h"

void bar(void)
{
  printf("myvar %d\r", myvar);
}


globals.h:
extern int myvar;
  #5   Spotlight this post!  
Unread 02-02-2006, 11:07
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Using vars across multiple .c files

Quote:
Originally Posted by Smithvillefirst
I've tried this, but it won't work. In a file globals.h, I have the following:
int myvar;

Then, in user_routines.c I have:
#include "globals.h"
extern int myvar;
That's exactly backwards. You should put the extern declaration in the .h file, and actually define the variable in only one .c file.
  #6   Spotlight this post!  
Unread 02-02-2006, 12:38
Smithvillefirst Smithvillefirst is offline
Anthony Peck
FRC #1806 (Smithville First)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Smithville
Posts: 9
Smithvillefirst is an unknown quantity at this point
Re: Using vars across multiple .c files

That's what I was looking for. Everything compiled just fine. Thanks a bunch!

Quote:
Originally Posted by Alan Anderson
That's exactly backwards. You should put the extern declaration in the .h file, and actually define the variable in only one .c file.
__________________
Anthony Peck
Integration Manager
Team 1806
  #7   Spotlight this post!  
Unread 02-02-2006, 20:29
mogunus mogunus is offline
Registered User
AKA: Luciano DiFalcone
FRC #1350 (Rambots)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2003
Location: RI
Posts: 19
mogunus is an unknown quantity at this point
Re: Using vars across multiple .c files

Alternatively, you could use pointers. Declare variables in main.c, declare pointers to them, and pass said pointers to the ProcessDatefrommasterUP and processdatafrommasterIO functions. Then pass them from there to whatever other functions you want to have access to those vars.
__________________
... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
-- Robert Firth
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using eclipse dasRatsel Programming 82 02-01-2006 13:20
Multiple Files EricWilliams Programming 4 30-03-2005 15:51
Combining multiple objects in different files into the same scene and file? DinkyDogg 3D Animation and Competition 8 19-02-2005 19:14
problem with .c files incognito_NICK Programming 5 31-01-2005 13:19
Multiple files usage punarhero Programming 2 12-01-2003 11:45


All times are GMT -5. The time now is 18:06.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi