• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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)

0
AzizLight
Asked:
AzizLight
  • 5
  • 5
  • 3
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now