By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

Problem with include header file

P: n/a
Hi, every body.

I have 3 files like this:
--------------------------------------------------------
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<iostream>
#include "header.h"
using namespace std;
void fn1()
{
cout<<a;
}
---------------------------------------------------------
file name : file2.cpp

#include<iostream>
#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?

Thank you in advance

Chat

May 25 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Ico
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<iostream>
#include "header.h"
using namespace std;
void fn1()
{
cout<<a;
}
---------------------------------------------------------
file name : file2.cpp

#include<iostream>
#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

May 25 '06 #2

P: n/a
"chat" <ch***********@gmail.com> writes:
Hi, every body.

I have 3 files like this: [snip] file name : file1.cpp

#include<iostream>
#include "header.h"
using namespace std;
void fn1()
{
cout<<a;
}

[snip]

This isn't C.

comp.lang.c++ is down the hall, past the water cooler, first door on
the left.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 25 '06 #3

P: n/a
chat wrote:
Hi, every body.

I have 3 files like this:
--------------------------------------------------------
file name : header.h

#ifndef TEST_H
#define TEST_H
int a=1;
double b=0.5;
having defining declarations (or declarations at all) of variables in a
header file is a vety bad idea.
void fn1();
#endif
---------------------------------------------------------
file name : file1.cpp

#include<iostream>


And now you have left C entirely. I suspect you wanted
<news:comp.lang.c++>, where some other language is discussed.
May 25 '06 #4

P: n/a
Thank you very much Ico. It helps me a lot. You said "the #ifdef's only
make sure that a single header file is not include twice *from the
same source*". I wonder why we need to assure that a single header
file must be included only one? Because we usually include a single
header file only one for each source. Or there are another resons?

Thank you again for your answer.

Chat

May 25 '06 #5

P: n/a
Ico said:
void main()


I know you were merely quoting someone else's code. But of course the return
type of main is int. People who are not prepared to learn /that/ are
probably not prepared to learn anything we try to teach them, so I
recommend that you start off with that.

Nowadays, if I reply to a void mainer at all, that is likely to be the only
crit I give for their code. If they can't even be bothered to get the entry
point right, what's the point?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 25 '06 #6

P: n/a
chat wrote:
Thank you very much Ico. It helps me a lot. You said "the #ifdef's only
make sure that a single header file is not include twice *from the
same source*". I wonder why we need to assure that a single header
file must be included only one? Because we usually include a single
header file only one for each source. Or there are another resons?


First, please see <http://cfaj.freeshell.org/google/>, it helps
people work out what you're talking about when they can see what
you're replying to.

As for your question, the most common use is when you have
two headers, one which depends on the other.

a.h:
struct {
int a;
} abc;

b.h:
struct abc fooify (struct abc);

And you have a program which needs to use fooify. You have
three choices:
1. Always include a.h then b.h. This is tedious.
2. Combine a.h and b.h (or make b.h include a.h and never
include a.h on its own). Fine for the above example,
and may often make sense, but if a.h represents some
basic functionality and b.h is a more complex interface
built on it this doesn't always make sense.
3. Make b.h include a.h, but use include guards with a.h,
this way the user of the headers (+libraries) doesn't
have to worry about including both or just b.h when
they need b.h, and can include a.h on it's own if they
just need a.h. Some C implementations use this approach
to allow standard library headers to be included in any
order (which the standard requires I believe).

--
imalone
May 25 '06 #7

P: n/a
Thank you Ian Malone and all other suggestions. It make me more
understand about what conditional compilation used for and how
conditional compilation prevent including header file twice.

Cheer!

Chat

May 25 '06 #8

P: n/a
Martin Ambuhl wrote:
having defining declarations
I think you meant "having defintions"
(or declarations at all)
of variables in a header file is a vety bad idea.


Declarations of variables with the extern keyword,
are appropriate in header files.

--
pete
May 25 '06 #9

P: n/a
chat wrote:

I have 3 files like this:
--------------------------------------------------------
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<iostream>
#include "header.h"
using namespace std;
void fn1()
{
cout<<a;
} .... snip ...
What is the mistake? Why are variable a,b multiple defined since
I use compiler directive to assure the multiple include header file?


You have two fundamental problems. First you are declaring data
objects in header files, which is a no-no. Second, you are using
C++, which is off-topic here. We deal with C.

The purpose of headers is to export connections to another source
file.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 25 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.