Go to Post You will find we are more talkative in about 5 1/2 weeks. ;) - mrnoble [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 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;
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 06:31.

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