chat <ch***********@ gmail.com> wrote:
I have 3 files like this:
--------------------------------V------------------------
file name : header.h
#ifndef TEST_H
#define TEST_H
int a=1;
double b=0.5;
void fn1();
#endif
---------------------------------------------------------
file name : file1.cpp
#include<iostre am>
#include "header.h"
using namespace std;
void fn1()
{
cout<<a;
}
---------------------------------------------------------
file name : file2.cpp
#include<iostre am>
#include "header.h"
using namespace std;
void main()
{
fn1();
}
----------------------------------------------------------
I use vc++ 6.0 compile all the files and no error occur. The problem
occur when I build program.
The error message are:
file2.obj : error LNK2005: "double b" (?b@@3NA) already defined in
file1.obj
file2.obj : error LNK2005: "int a" (?a@@3HA) already defined in
file1.obj
Debug/test.exe : fatal error LNK1169: one or more multiply defined
symbols found
What is the mistake? Why are variable a,b multiple defined since I use
compiler directive to assure the multiple include header file?
Not quite: the #ifdef's only make sure that a single header file is not
include twice *from the same source*. However, if you include the .h
from two different .c files, the contents will happily be included, with
or without #ifdef. Remenber: the compiler only works with one source
file at a time, and does not know what happend with the previous file,
or what will happen with the next. At the end, the linker is putting
everything together, and notices the double declarations.
After processing by the precompiler, your source files look something
like this :
---------------------------------------------------------
file name : file1.c
#include <stdio.h>
extern int a;
extern double b;
void fn1();
void fn1()
{
printf("%d\n", a);
}
---------------------------------------------------------
file name : file2.cpp
#include <stdio.h>
extern int a;
extern double b;
void fn1();
void main()
{
fn1();
}
----------------------------------------------------------
(Note that I changed your code to C, since talking about C++ code on
comp.lang.c is generally considered Bad Practice)
As you can see, you are actually declaring int a and double b in both
source files, so your compiler was right complaining at you.
Note that declaring variables or functions in header files is generally
considered Bad Practice as well, for the exact reason you are
experiencing here. Declare your variables and functions in .c files,
and only use the header files for the definitions. Try something like
this :
---------------------------------------------------------
file name : header.h
#ifndef TEST_H
#define TEST_H
extern int a;
extern double b;
void fn1();
#endif
---------------------------------------------------------
file name : file1.c
#include <stdio.h>
#include "header.h"
void fn1()
{
printf("%d\n", a);
}
---------------------------------------------------------
file name : file2.cpp
#include <stdio.h>
#include "header.h"
int a = 2;
double b = 0.5;
void main()
{
fn1();
}
----------------------------------------------------------
--
:wq
^X^Cy^K^X^C^C^C ^C