finctions in a lib...

Hi,
I want to make a dll with +- the following two functions (It's a sample  problem to point out what I want. I know that the following code is dumb)
-------------------------------------
void f1(int i)
{
int j =5 + i;
}

void f2()
{
\\here I want to do stj with int j, which is defined in f1
}
--------------------------------------
is this possible ??
How should I do this?? (I know allready how to make really simple dll's)

AzizLightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pb_indiaCommented:
you will have to make j variable global or declare it in the beginning before definging f1() and f2()
0
pb_indiaCommented:
//Example

int j=0;

void f1(int i)
{
 j =5 + i;
}

void f2()
{
j=f1(10)+5;


}
0
AzizLightAuthor Commented:
Thanks, but what if the j is calculated (I think you can't place code (not-initialisation) out of functions for making a dll, isn't that correct) ??
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

AzizLightAuthor Commented:
I'm trying to compile the code for the following dll and got these errors:
code:
--------
#include "dlltest.h"
#define MAXMODULE 50
char module[MAXMODULE];

extern "C" __declspec(dllexport)
void f1()
{int k = 22;
cout << "f1"<<endl;
}

extern "C" __declspec(dllexport)
void f2()
{cout << "f2 : " << k <<endl;
}
--------------
error:
'k' : undeclared identifier
...
0
rstaveleyCommented:
Forget about DLLs for a bit. Your error message is simply telling you that k can't be seen from f2. Its scope is limited to f1.
0
pb_indiaCommented:
Use this:
You cannot declare "k" in one function and use in another. You have to make it globally available as below:

#include "dlltest.h"
#define MAXMODULE 50

char module[MAXMODULE];
int k=0;
extern "C" __declspec(dllexport)
void f1()
{
k = 22;
cout << "f1"<<endl;
}

extern "C" __declspec(dllexport)
void f2()
{
cout << "f2 : " << k <<endl;
}
0
rstaveleyCommented:
Follow pb_india's recommendation if you want k's scope to span all functions in the module.
0
AzizLightAuthor Commented:
Thanks, both,
but to come back to my question above.... (sorry if I'm nagging)
what if the j is calculated (I think you can't place code (not-initialisation) out of functions for making a dll, isn't that correct) ??
I know that you can first initialise everything global, and the make a function CalculateAtInitialisation(initialised data) or sth like that, but I have lots of variables to initialise, so it would be nice if there is a keyword or something like that that can make that youre data is also available for other functions....
0
pb_indiaCommented:
You can use a class and declare variable as private.
Then you can write "get" and "set" functions to access the variable.
That way the data will be availabe to all functions.

I don;t celarly understand what you mean here:
what if the j is calculated (I think you can't place code (not-initialisation) out of functions for making a dll, isn't that correct) ??

You can certainly have code out side functions.
0
AzizLightAuthor Commented:
"You can certainly have code out side functions." yes, but for a dll you don't have a main loop (which is also a function, actually (?!)), so where should  you place the "global code" ??

thanks.
0
pb_indiaCommented:
You can have code in the dll_main where you attach and detach threads and processes.

You certainly don;t have a main fucntion like an exe.
Like I suggested you can declare all the variable as private or if you declare public, have a common initialization function that initializes them all.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AzizLightAuthor Commented:
Hi,
could you perhaps give some sample (pseudo)code of a dll-cpp-file where there are some calculations done in the "initialisation" function??
(sorry I'm so dumb ...)
0
rstaveleyCommented:
Points to pb_india though he didn't respond to the follow-on question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.