![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Location: new zealand
Posts: 2
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
|
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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;
}
#endifmyInclude::variable |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2005
Location: new zealand
Posts: 2
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|