473,890 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to avoid multiple definition of a variable by multiple inclusion of a header file

Easily described problem:

Using g++ version 3.3.5 under suse 9.3,

bla.h:
-----------
#ifndef myTEST
#define myTEST
ZFSInt test;
#endif
------------

leads to an error at link time...
application.o(. bss+0x0):/path_here/application.cpp :19: multiple
definition of `test'
app.o(.bss+0x0) :/path_here/app.cpp:17: first defined here
....for EVERY call of #include "bla.h"
:(

Apparently the #ifndef only works at compile time, but somehow causes
an error at linktime. Any solution? I want the variable "test"
available in all modules that #include "bla.h"

Regards,

Lars Uffmann

Aug 1 '06
11 22596
Hi,

In my implementation, it is like

#ifndef TEST
#define TEST
const char* strDescriptions[] = {"Desc1", "Desc2", "Desc3"};
#endif

How to split the definition and definition of the above.

Regds,
Chinthan EP
la**********@rw th-aachen.de wrote:
Mi************* @tomtom.com schrieb:
This has nothing to do with #ifdef, which is a preprocessor command.
It works only at compile time, and skips every second defintion of 'test'
*in a single .cpp*, not across .cpp's. As the compiler works on single
.cpp's it won't notice that.

Woohoo - I see light in the dark! Finally the explanation I've been
waiting for :) To be honest, I suspected something like this, because
the compiler creates libraries for each cpp file separately, so the
linker would spot multiple declarations of the variable, but in that
case the sense of the preprocessor commands did not occur to me - I
never realized it was only to skip further definitions within the same
cpp-File.

Thanks for clearing this up! :)
And to all the others that helped solving the problem!

Regards,

Lars
Aug 5 '06 #11
ch********@gmai l.com wrote:
Hi,

In my implementation, it is like

#ifndef TEST
#define TEST
const char* strDescriptions[] = {"Desc1", "Desc2", "Desc3"};
#endif

How to split the definition and definition of the above.
First, please don't top-post on Usenet, your reply belongs below or
interleaved with the post you are replying to.

to split, declare the variable in a header and define in once and once
only in a source module.

In test.h:

extern const char* strDescriptions[];

in somefile.cc:

const char* strDescriptions[] = {"Desc1", "Desc2", "Desc3"};

--
Ian Collins.
Aug 5 '06 #12

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

Similar topics

2
21121
by: Jochen Zeischka | last post by:
Hi everybody! I have a question concerning code organisation. Suppose I have the following header file: #ifndef SOME_NAME #define SOME_NAME namespace N { void F()
9
10287
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not only seems to be a bad practice but also creates a bug on MacOSX panther (gcc 3.3).
2
9092
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is applied to an external variable or function or to an automatic variable. When applied to an external variable (global) variable the scope of that variable to the file in which it is declared. This is useful to hide buffers and variables that are used...
6
5213
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
10
2818
by: zfareed | last post by:
Similar problem to the previous post. I have created a project with about 7 files including 3 header files and 3 implementation files. I am getting a multiple definition error when compiling for one specific file of class. I have checked that if there is more than one allocation of storage for identifiers, this error arises. But I don't see where the problem lies. Any suggestions? <code> //ItemType.h #ifndef ItemType_hpp // I was...
8
7640
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will be successful but not linking. Can somebody help me in telling the exact reason why compiler cannot find the reference of template class member function definition defined in cpp file?? following problem is not linking (undefined reference of...
11
2933
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is this possible.I thought it will throw "Variable redefinition Error". Giving the source code for reference.Please help me out on this... a.h ----- int g; void fun(void);
7
3005
by: bcpkh | last post by:
Hello All Received a header file from a supplier that defines an interface to implement but it's giving me a problem, I reproduce the general structure of the header file below; #ifndef XYZ_H various #define(s)
2
1468
by: sayeo87 | last post by:
I am having VERY weird behavior with a variable in my header file. Basically I have one .c file and one .h file where I store all definitions and prototypes. I have a "#define INFINITY 15000" macro in there which worked fine in my program until out of the blue it just decided not to work. Doing "int x = INFINITY" would yield garbage values instead. Very puzzled, I thought maybe the compiler wasn't detecting changes in my .h file or something...
0
9980
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
10830
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
10925
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
10468
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...
0
9641
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7172
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();...
0
5855
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6061
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.