473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Making a global variable

I'm modifying a pre-existing programme with a defined project
structure. I need to add a global variable that is visible in both an
implementation file in a library, and visible inside the main of a
programme that uses that library. This seems to be impossible, as any
approach I take either results in the variable being invisible in one
file, or the linker complaining about multiple definition. I'm using MS
VC++ 6.

The two basic approaches I've taken have been to either define the
variable in a header file, which results in multiple definition
complaints, or to try using extern declarations, which results in mere
invisibility.

Please help.

Jul 23 '05 #1
9 1637
I should mention that I have tried including the variable in a
namespace, under both approaches, as well as not.

Jul 23 '05 #2
<ro*******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I'm modifying a pre-existing programme with a defined project
structure. I need to add a global variable that is visible in both an
implementation file in a library, and visible inside the main of a
programme that uses that library. This seems to be impossible, as any
approach I take either results in the variable being invisible in one
file, or the linker complaining about multiple definition. I'm using MS
VC++ 6.

The two basic approaches I've taken have been to either define the
variable in a header file, which results in multiple definition
complaints, or to try using extern declarations, which results in mere
invisibility.

If the library is statically linked, your latter approach
should have worked and its failure to do so will have
to be diagnosed with the benefit of seeing some code.

If the library is dynamically linked, your problem is
off-topic here and should be taken to one of the
groups in the microsoft.public.* hierarchy.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #3

<ro*******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I'm modifying a pre-existing programme with a defined project
structure. I need to add a global variable that is visible in both an
implementation file in a library, and visible inside the main of a
programme that uses that library. This seems to be impossible, as any
approach I take either results in the variable being invisible in one
file, or the linker complaining about multiple definition. I'm using MS
VC++ 6.

The two basic approaches I've taken have been to either define the
variable in a header file, which results in multiple definition
complaints, or to try using extern declarations, which results in mere
invisibility.

Please help.


I'm a bit confused about exacytly what you have tried, since you don't show
any code. But in general, assuming I was forced to use a global variable in
the first place (yuk), I'd declare the variable as extern in a common header
in my library (which implementation files in both the library and the
program will include), and define it in the implementation file in the
library.

(I suspect this is in the FAQ, but I'm too lazy to look myself right now.
:-))

-Howard
Jul 23 '05 #4

Larry Brasfield wrote:
If the library is statically linked, your latter approach
should have worked and its failure to do so will have
to be diagnosed with the benefit of seeing some code.


Indeed, it is statically linked. In a development that increasingly
makes me hate computers in all ways, this seems to have suddenly
started to work, after a mere 3 hours of doing substantially the same
thing, interleaved with reading confirming my approach.

Thanks for your help, and for responding so soon.

Jul 23 '05 #5
header file :

#ifndef GLOGAL
#define GLOBAL

int Globalvariable;

#endif
Jul 23 '05 #6
header file :

#ifndef GLOGAL
#define GLOBAL

int Globalvariable;

#endif


This won't help when the header is included in multiple source files, will it ?

Multiple definition, reported by the linker this time...

Cheers, Jaap
Jul 23 '05 #7
Jaap Versteegh schrieb:
header file :

#ifndef GLOGAL
#define GLOBAL

int Globalvariable;

#endif

This won't help when the header is included in multiple source files,
will it ?

Multiple definition, reported by the linker this time...

Cheers, Jaap


Just do it like Howard said:

"I'd declare the variable as extern in a common header
in my library (which implementation files in both the library and the
program will include), and define it in the implementation file in the
library."

In code:

// testing.h
#ifndef TESTING_H
#define TESTING_H

extern int i;

void foo();

#endif

// testing.cpp
#include "testing.h"

int i;

void foo()
{
i = 6;
}

// main.cpp
#include "testing.h"
#include <iostream>
using namespace std;

int main()
{
foo();
cout << i;
}
Jul 23 '05 #8
Dont confuse the header file, for a cpp file.
The header is only there to allow multiple file projects to have a common
interface before linking.

You should give info about the global variable in header:

extern int Globalinfo;

while keeping the Actual code in the cpp :

int Globalinfo;

And make sure you 1. include the header, and 2. link the code.

Link the code once, and once only, and there is only one copy of the data,
howver you can include headers all day long, this tells the other code that
there will be a copy of the data at link time.

I see a lot of people putting code in header files, while that may work in
some cases, it is a bad habit to get into, and will cause problems sooner or
later.
Jul 23 '05 #9
DHOLLINGSWORTH2 schrieb:
Dont confuse the header file, for a cpp file.
The header is only there to allow multiple file projects to have a common
interface before linking
You should give info about the global variable in header:

extern int Globalinfo;

while keeping the Actual code in the cpp :

int Globalinfo;

And make sure you 1. include the header, and 2. link the code.

Link the code once, and once only, and there is only one copy of the data,
howver you can include headers all day long, this tells the other code that
there will be a copy of the data at link time.

I see a lot of people putting code in header files, while that may work in
some cases, it is a bad habit to get into, and will cause problems sooner or
later.


Eh, I'm practising your tips or do I missunderstand you?
Jul 23 '05 #10

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

Similar topics

0
by: |-|erc | last post by:
Hi! Small challenge for you. The index.php uses this file and calls layout(). Take a look at www.chatty.net this file draws the chat login box on the right. I traced the CHAT button it submits...
7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
3
by: Johannes Zellner | last post by:
Hi, can I make an object read-only, so that x = new_value fails (and x keeps it's orginal value)? This would be especially of interest for me for an object created by a c extension.
9
by: windandwaves | last post by:
Hi gurus I have a class from which I create an object. I want this object to be available in all my functions for my application. Basically, I want to make the object global. What is the best...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
Markus
by: Markus | last post by:
This is for you pb - you're looking a little bored; answering all those old threads ;) So here's what ive got, it's a validation function: <?php /* *$username = $_POST; *$email = $_POST; *...
204
by: Masood | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am...
1
by: manuitpro | last post by:
I am using class and and connection variable $this->_connection = @fsockopen($this->_hostname, 23, $errno, $errstr, $this->_timeout), class has other variables also. class object is global, and when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.