473,396 Members | 1,789 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,396 software developers and data experts.

extern variable

I used to have a program(A.EXE) using an extern variable as follows:

A.cpp
extern DWORD Flag;

B.cpp
DWORD Flag;

Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.

I am fine/done with 1st step but got stuck with the 2nd task (being a
newbie)!!
Can someone shed some light here? (hopefully with an example)

P.S. I tried exporting the variable (Flag) in the B.DEF file but
couldn't move further.

Thanks in advance.
- VV

Dec 24 '06 #1
4 5012
va*****@gmail.com wrote:
I used to have a program(A.EXE) using an extern variable as follows:

A.cpp
extern DWORD Flag;

B.cpp
DWORD Flag;

Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.
DLLs and linking are beyond the scope of the language. I recommend a
newsgroup dedicated to your platform and/or development system.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Dec 24 '06 #2
<va*****@gmail.comwrote in message
news:11**********************@i12g2000cwa.googlegr oups.com...
>I used to have a program(A.EXE) using an extern variable as follows:

A.cpp
extern DWORD Flag;

B.cpp
DWORD Flag;

Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.

I am fine/done with 1st step but got stuck with the 2nd task (being a
newbie)!!
Can someone shed some light here? (hopefully with an example)

P.S. I tried exporting the variable (Flag) in the B.DEF file but
couldn't move further.

Thanks in advance.
- VV
Your question is pretty much off topic, but on topic to a certain extent.
Basically, however, what you want to do is OS specfic. extern works at link
time. A.cpp will use the same Flag from B.cpp when the objects are linked.
What you are talking about using the same variable in a different program (a
dll at this point) is run time. Ask in a newsgroup specific to your OS
(probably windows) and you'll probably be pointed to shared memory. Try
comp.os.ms-windows-programmer-win32 or the like.
Dec 25 '06 #3
Jim Langston <ta*******@rocketmail.comwrote:
><va*****@gmail.comwrote in message
>A.cpp
extern DWORD Flag;

B.cpp
DWORD Flag;

Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.

I am fine/done with 1st step but got stuck with the 2nd task (being a
newbie)!!
Can someone shed some light here? (hopefully with an example)
Your question is pretty much off topic, but on topic to
a certain extent. Basically, however, what you want to do
is OS specfic. extern works at link time. A.cpp will use the
same Flag from B.cpp when the objects are linked. What you are
talking about using the same variable in a different program
(a dll at this point) is run time. Ask in a newsgroup specific
to your OS (probably windows) and you'll probably be pointed
to shared memory.
I would try to avoid creating a need for functions in libraries
(dynamic or otherwise) and functions in the main executable
from needing to access the same global variables. To do so
would be poor encapsulation. It's much better if the library function
can work completely from its arguments, and the calling function
gets everything it needs from the return value of the library
function.

If you need a persistent state between function calls, consider
using a static variable.

Just an opinion.

Steve
Dec 25 '06 #4
Thank you all..

On Dec 25 2006, 1:20 am, spop...@speedymail.org (Steve Pope) wrote:
Jim Langston <tazmas...@rocketmail.comwrote:
<vadn...@gmail.comwrote in message
A.cpp
extern DWORD Flag;
B.cpp
DWORD Flag;
Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.
I am fine/done with 1st step but got stuck with the 2nd task (being a
newbie)!!
Can someone shed some light here? (hopefully with an example)
Your question is pretty much off topic, but on topic to
a certain extent. Basically, however, what you want to do
is OS specfic. extern works at link time. A.cpp will use the
same Flag from B.cpp when the objects are linked. What you are
talking about using the same variable in a different program
(a dll at this point) is run time. Ask in a newsgroup specific
to your OS (probably windows) and you'll probably be pointed
to shared memory.I would try to avoid creating a need for functions in libraries
(dynamic or otherwise) and functions in the main executable
from needing to access the same global variables. To do so
would be poor encapsulation. It's much better if the library function
can work completely from its arguments, and the calling function
gets everything it needs from the return value of the library
function.

If you need a persistent state between function calls, consider
using a static variable.

Just an opinion.

Steve
Jan 30 '07 #5

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

Similar topics

10
by: mark | last post by:
compiler g++ on linux file.h has the following, int x=5; and inside the main program file I can access "x" without declaring it "extern x". Program compiles and runs fine.... also program...
2
by: Sona | last post by:
Hi, I read some place that if you have two or more modules or files in C, you can share variables between the these modules or files using the 'extern' keyword. I'm not really sure how to do...
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
4
by: gaurav.dube | last post by:
I am facing a problem with warning removal I have got a header file it contains extern declarations of lot of variables (say 100) This header file is included in hundreds of .c files. Some of...
9
by: Lalatendu Das | last post by:
I have seen a header file in which one structure is defined with extern declaration in a header file and declared one variable of that structure in one C-file. The code goes as mentioned below . I...
2
by: Shraddha | last post by:
When I read about volatile keyword....while explaning there was an declaration like... extern const a; But as a rule const shoule be initialised where it is defined...but we do ot initialise...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...
0
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,...

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.