473,602 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with include header file

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

Thank you in advance

Chat

May 25 '06 #1
9 4036
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<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

May 25 '06 #2
"chat" <ch***********@ gmail.com> writes:
Hi, every body.

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

#include<iostre am>
#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_Keit h) 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
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<iostre am>


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
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
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
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
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
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
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<iostre am>
#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.c om, 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/googlegroupsrep ly/>
May 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2636
by: Bartosz Wegrzyn | last post by:
I need help with sessions. I createt set of web site for nav with authorization. first I go into main.php which looks like this: <?php //common functions include_once '../login/common.php'; global $LOGINDIR;
0
2385
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do automake, if i do that in the proper director it says "automake: 'configure.ac' or 'configure.in' is required" i dont know where to bring those files. secondly if i only compile simple1.cc using gcc proving all the link flags to libraries and...
5
1274
by: Russoue | last post by:
I am having a strange problem. I have a static int with an initial value in a header file which I include from two source files. In one file, I assign a value to that int but the other file don't get it. I have printed the value from both files. The first line printed from the first file shows the assigned value but the second line printed from the second file shows the initial value. How can this happen? Here is an example: File:...
6
7056
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this header file. The function is stored in C:\temp\myfun.c int func(){ return 1;
0
3924
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
102
6751
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem never to send if-modified-since. But the strange thing is that Opera sends if-modified-since but when I reply with "HTTP/1.0 304 Not Modified" it is not refreshing the screen from its cache, it is leaving the screen blank.
5
2219
by: Tio | last post by:
I have project in MFC(vc++) . There are files and classes: classes:dialog1,dialog2,aaa,bbb ---------------------- main.cpp --------------------- #include "mainfrm.h" #include "dialog1.h" #include "dialog2.h"
7
1754
by: The Cool Giraffe | last post by:
I have the following: //A.h class A {}; //B.h #include "A.h" class B : A {};
9
1914
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.