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. 9 1637
I should mention that I have tried including the variable in a
namespace, under both approaches, as well as not.
<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.
<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
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.
header file :
#ifndef GLOGAL
#define GLOBAL
int Globalvariable;
#endif 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
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;
}
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.
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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.
|
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...
|
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
|
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...
|
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;
*...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |