Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with include header file

Reply
Thread Tools

Problem with include header file

 
 
chat
Guest
Posts: n/a
 
      05-25-2006
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

 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      05-25-2006
chat <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-25-2006
"chat" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-25-2006
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.
 
Reply With Quote
 
chat
Guest
Posts: n/a
 
      05-25-2006
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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-25-2006
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)
 
Reply With Quote
 
Ian Malone
Guest
Posts: n/a
 
      05-25-2006
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
 
Reply With Quote
 
chat
Guest
Posts: n/a
 
      05-25-2006
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

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-25-2006
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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-25-2006
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/>


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
include file in include file PTM HTML 1 11-12-2007 10:13 AM
#include "file" -vs- #include <file> Victor Bazarov C++ 4 03-06-2005 07:09 PM
#include headers that include this header Aguilar, James C++ 2 07-16-2004 05:56 PM



Advertisments