Link to home
Start Free TrialLog in
Avatar of AzizLight
AzizLight

asked on

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)

Avatar of pb_india
pb_india

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

int j=0;

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

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


}
Avatar of AzizLight

ASKER

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) ??
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
...
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.
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;
}
Follow pb_india's recommendation if you want k's scope to span all functions in the module.
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....
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.
"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.
ASKER CERTIFIED SOLUTION
Avatar of pb_india
pb_india

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ...)
Points to pb_india though he didn't respond to the follow-on question.