Problem with include header file

C

chat

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
 
I

Ico

chat said:
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();
}
----------------------------------------------------------
 
K

Keith Thompson

chat said:
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.
 
M

Martin Ambuhl

chat said:
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

And now you have left C entirely. I suspect you wanted
<where some other language is discussed.
 
C

chat

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
 
R

Richard Heathfield

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?
 
I

Ian Malone

chat said:
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).
 
C

chat

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
 
P

pete

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.
 
C

CBFalconer

chat said:
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/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top