Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 26th, 2005, 5:26 PM   #1
carlgreen
Newbie
 
Join Date: Feb 2005
Location: new zealand
Posts: 2
Rep Power: 0 carlgreen is on a distinguished road
multiple definition of variables in include files

Hi,

I have been having trouble with defining global variables in a header file and including this in other files. It seems that every way I do it so that I can compile the code, the linker gives me a multiple definition error.

Here is a simplified version if anyone can help me out:

/* main.c */

#include "include.h"
#include "a.h"

int main(int argc, char *argv[]) {
  variable = 1;
  return 0;
}

/* include.h */

#ifndef INCLUDE_H
#define INCLUDE_H

int variable = 0;

#endif

/* a.h */

#include "include.h"

int getVariable();

/* a.c */

#include "a.h"

int getVariable() {
  return variable;
}

This is the output from the compiler:

gcc.exe -c main.c -o main.o -I"E:/Dev-Cpp/include"
gcc.exe -c a.c -o a.o -I"E:/Dev-Cpp/include"
gcc.exe main.o a.o -o "c.exe" -L"E:/Dev-Cpp/lib"
a.o(.data+0x0):a.c: multiple definition of `variable'
main.o(.data+0x0):main.c: first defined here

If anyone can tell me what I am doing wrong I would greatly appreciate it.
carlgreen is offline   Reply With Quote
Old Feb 26th, 2005, 6:03 PM   #2
TerraNerd
Programmer
 
TerraNerd's Avatar
 
Join Date: Jul 2004
Location: Vermont, USA
Posts: 65
Rep Power: 5 TerraNerd is on a distinguished road
Send a message via AIM to TerraNerd
I dont know any C or C++, but from one of the errors, (a.o(.data+0x0):a.c: multiple definition of `variable') i can see whats going on. Your assigning 'variable' two different values at once.

When your telling 'main.c' to include 'incude.h' variable is being defined twice, because the documents 'merge,' if you will.

/* main.c */

#include "include.h"
#include "a.h"

int main(int argc, char *argv[]) {
  variable = 1;
  return 0;
}

/* include.h */

#ifndef INCLUDE_H
#define INCLUDE_H

int variable = 0;

#endif


The integer 'variable' is being assigned 0 as well as 1.
__________________
[See a gallery of my Graphic art Here.][Visit my website Here]
TerraNerd is offline   Reply With Quote
Old Feb 26th, 2005, 6:35 PM   #3
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
there are 3 ways you can fix that:
a) change the names of your variables so they dont repeat
b) put the variable in a class
c) define a namespace for your variable

namespace example:
/* include.h */

#ifndef INCLUDE_H
#define INCLUDE_H
namespace myInclude {
   int variable = 0;
}
#endif
and then you'd access variable as
myInclude::variable
ZenMasterJG is offline   Reply With Quote
Old Feb 26th, 2005, 7:02 PM   #4
carlgreen
Newbie
 
Join Date: Feb 2005
Location: new zealand
Posts: 2
Rep Power: 0 carlgreen is on a distinguished road
TerraNerd, your suggestion worked for this example. I changed the declaration to int variable; and it works. I would have thought it would just get set to 0, and then to 1.

but

My original problem was in a c++ program. So this example now works when I use gcc, but if I try it with g++ I still get pretty much the same error:

g++ -o main.exe main.o a.o
a.o(.bss+0x0):a.c: multiple definition of `variable'
main.o(.bss+0x0):main.c: first defined here

ZenMasterJG,

I don't think (a) will work, because I want this to be one global variable. I tried (c), but it seems that the section:

namespace myInclude {
   int variable = 0;
}

is being carried out twice. My understanding was that the ifndef would stop that, but I guess that must only work at the compiling stage?

I guess I should really be doing it like (b) and setting the values in the classes that I want it.
carlgreen is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC