Página 1 de 1

GLOBAL e GLOBAL EXTERNAL

Enviado: 14 Set 2016 21:46
por asimoes
Pesquisando,

Achei este tipo no documento do xHarbour, isso existe no harbour?

GLOBAL <varName> [:= <xValue> ]
GLOBAL EXTERNAL <extVarName>

GLOBAL <varName>
<varName> is the symbolic name of the global variable to declare.
<xValue>
<xValue> is an optional value to assign to the GLOBAL variable after being declared. To assign a value, the inline assignment operator (:=) must be used. The simple assignment operator (=) cannot be used. Only literal values are allowed for <xValue> when declaring GLOBAL variables.
GLOBAL EXTERNAL <extVarName>
This is the name of a GLOBAL variable that is declared in another PRG file and must be referred to in the current PRG file.

GLOBAL e GLOBAL EXTERNAL

Enviado: 16 Set 2016 08:58
por Vlademiro
Eu testei aqui e não funcionou com Harbour 3.2

Testei dessa forma e ele nem compilou.

global.prg

Código: Selecionar todos

GLOBAL v := "Teste"

PROCEDURE Main
//GLOBAL v := "Teste"

   ? V
   ? PrcExterna()

RETURN
prc.prg

Código: Selecionar todos

GLOBAL EXTERNAL v
PROCEDURE PrcExterna()

   ? v

RETURN

GLOBAL e GLOBAL EXTERNAL

Enviado: 16 Set 2016 17:19
por asimoes
Acho que isso é exclusivo do xharbour

GLOBAL e GLOBAL EXTERNAL

Enviado: 16 Set 2016 18:30
por JoséQuintas
Também cheguei a testar se isso funcionava.
Já teria algum uso pra isso?
Resolvi com funções.

GLOBAL e GLOBAL EXTERNAL

Enviado: 16 Set 2016 18:57
por asimoes
Achei essa informação:


GLOBAL variables:

GLOBAL <Id1> [,<Id2> [,<IdN>]]

GLOBAL Variables are a new kind of a declared variables. they have PUBLIC like visibility, but are faster than even STATICs. GLOBALS can be referenced from other modules using the syntax:
GLOBAL EXTERNAL <Id1> [,<Id2> [,<IdN>]]

GLOBAL Variables have the added benefit of being *directly* accessible from C code too.

Sample code:

Código: Selecionar todos


//-------------------------------------//
GLOBAL g_MyObject
#include "hbclass.ch"
PROCEDURE Main()
   g_MyObject := MyClass()
   MyCFunction()
RETURN

CLASS MyClass
   METHOD MyMethod INLINE Alert( "MyMethod" )
ENDCLASS

#pragma BEGINDUMP
HB_FUNC( MYCFUNCTION )
{
   hb_objSendMsg( &G_MYOBJECT, "MyMethod", 0 );
   // Note must be ALL-CAPS!
}
#pragma ENDDUMP
//-------------------------------------//

GLOBAL e GLOBAL EXTERNAL

Enviado: 16 Set 2016 19:10
por asimoes
Bom, as minha duvidas foram esclarecidas:

### GLOBAL / GLOBAL EXTERNAL (GLOBAL_EXTERN) ###
======================================================
xHarbour support application wide static variables called GLOBALs.
To declare GLOBAL variable you have to put
GLOBAL <varname> [ := <initValue> ]
before the 1-st function in compiled .prg module and use -n compiler
switch. In xHarbour GLOBAL variables cannot be declared inside function
body. If user wants to use GLOBAL variable in different .prg module
then he has to add declaration
GLOBAL EXTERNAL <varname>
to his code before the 1-st function in compiled .prg module and use
-n compiler switch. Also, GLOBAL EXTERNAL declaration cannot be used
in function body. Unlike other variables GLOBALs declared in xHarbour
reserve used names so they cannot be used in the same module in local
function declarations, f.e. xHarbour cannot compile code like:
GLOBAL var
GLOBAL EXTERNAL var2
func F1( var )
return var * 2
func F2()
FIELD var2
return var2
due to name conflicts. In general users should be careful using the same
names for different type of variables in xHarbour.
GLOBAL variables in xHarbour need static link time bindings so they
do not work with dynamically loaded PCODE functions from .hrb files
or shared libraries (.dll, .so, .sl, .dyn, ...). Just like STATICs
they cannot be accessed by macro compiler.
Harbour does not support GLOBALs.
Fonte:https://github.com/vszakats/harbour-cor ... b-diff.txt

GLOBAL e GLOBAL EXTERNAL

Enviado: 16 Set 2016 19:48
por JoséQuintas
Na época entendi que seria igual variável pública, mas que poderia ser usada em multithread.
Exatamente esse foi meu interesse.

Mas já falamos disso em outro tópico.
Eu usei funções.

Código: Selecionar todos

FUNCTION AppVersao( xValue )
   STATIC x
   IF xValue != NIL
      x := xValue
   ENDIF
   RETURN x
Não precisa declarar variável, e o compilador avisa se o nome estiver errado.
Acaba sendo mais seguro.

Até pensei em declarar como classe, mas aí o compilador não vai avisar se errar o nome.
Ainda mais que meu computador está pedindo um teclado novo há bastante tempo, e de vez em quando as teclas falham e o teclado precisa de uma chacoalhada..... rs
Se não fosse a compilação -w3 -es2...... rs