embedding C code

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

how do you get a variable to exist 'globally' in C?
Normally, you just declare it outside of a procedure & it exists unless you declare another with the same name inside a block.

Code: Select all

! int i;
Procedure abc()
  Protected j
  !v_j = ++i;
  ProcedureReturn j
EndProcedure
Debug abc()
doesn't work.

Is there some documentation on the restrictions of embedded code?
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: embedding C code

Post by mk-soft »

Global module variables

[modulename]Xg_[variable]

Code: Select all

DeclareModule xyz
  Declare test()
EndDeclareModule
Module xyz
  Global myval2=1024
  Procedure test()
    Protected r1
    !v_r1 = xyzXg_myval2; 
    ProcedureReturn r1
  EndProcedure
EndModule
r1 = xyz::test()
Debug r1
Get this IDE-Tool: https://www.purebasic.fr/german/viewtop ... 93#p361093
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedding C code

Post by idle »

Take a look at my tool and the examples, raylib 4 for instance. Search for pbcex.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: embedding C code

Post by mk-soft »

jassing wrote: Thu Jun 08, 2023 4:03 am how do you get a variable to exist 'globally' in C?
Normally, you just declare it outside of a procedure & it exists unless you declare another with the same name inside a block.

Code: Select all

! int i;
Procedure abc()
  Protected j
  !v_j = ++i;
  ProcedureReturn j
EndProcedure
Debug abc()
doesn't work.

Is there some documentation on the restrictions of embedded code?
In the c-code, the procedures are written first, then the inline c-code outside of procedures. Thus the variable "i" is missing in the prcocedure.
See c-code output
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

mk-soft wrote: Thu Jun 08, 2023 8:44 am In the c-code, the procedures are written first, then the inline c-code outside of procedures. Thus the variable "i" is missing in the prcocedure.
out of order code - got it. thanks.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

mk-soft wrote: Thu Jun 08, 2023 8:19 am Global module variables

[modulename]Xg_[variable]
Thanks!

Seems you can exploit C to get variables declared outside of a module, too.
Post Reply