Hi guys,If I understood you correctly, you just want to use the ThreadStops variable in more than one unit. If that is the case, simply declare the ThreadStops variable in another unit (call it Globals.pas - if you want) which is then used by the other units where you want it to be accessible.
I have declared variable in lazarus project file, like this
var ThreadStops: boolean; ThreadsRunning: integer;
On my Mainform, I have a procedure button click
procedure TForm1.btnStartClick(Sender: TObject); var ThreadStops : boolean; cvar;external; begin end;
But it gave an errors :
Compile Project, Target: multithreadingexample1: Exit code 1, Errors: 2
mainunit.pas(85,26) Error: Cannot declare local variable as EXTERNAL
mainunit.pas(85,30) Fatal: Syntax error, ":" expected but ";" found
How to fix this?
Thanks in advance.
Here I go. :(
If you really want to use C++ style:
1-Declare your variable using cvar:
var test : integer; cvar;
2-Where you want to use it, add external to the same declaration:
var test: integer; cvar; external;
Now unlike usual Pascal variables, this is case sensitive.
Declare it with export:
test: integer; cvar; export;
In this case, I don't need a unit to holds my global variable.Just something for you to think about, having all the globals in one unit makes the maintenance of the code easier. It's much cleaner to have all the globals in one place/unit/(.h in the case of C/C++) than scattered all over.
In this case, I don't need a unit to holds my global variable.Just something for you to think about, having all the globals in one unit makes the maintenance of the code easier. It's much cleaner to have all the globals in one place/unit/(.h in the case of C/C++) than scattered all over.
That's what I am doing, I placed all global variables in project file, in C++, it is the bpr file.In Pascal (Delphi/FPC) the (sort of) equivalent is to have a separate unit for all the globals. The reason has to do with the fact that scoping is different in Pascal/FPC than in C/C++.
I am trying to do the same with Lazarus, put all global variables in its project file, the lpr file.
Thanks all for your help.You're welcome.