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

embedding C code

Post by jassing »

With the C backend, is it possible to embed C code like you can with ASM?
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: embedding C code

Post by Demivec »

jassing wrote: Tue Jun 06, 2023 10:50 pm With the C backend, is it possible to embed C code like you can with ASM?
Yes.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

OK, how do I pass a variable between PB and C?
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: embedding C code

Post by Demivec »

jassing wrote: Wed Jun 07, 2023 2:03 am OK, how do I pass a variable between PB and C?
The same as described in the help file under 'Inline x86 ASM'.

https://www.purebasic.com/documentation/reference/inlinedasm.html

Here's an excerpt:

Code: Select all

- The used variables or pointers must be declared before to use them in an assembler keyword. Their names in assembler are 'v_variablename' and 'p_pointername', and in a procedure their names are 'p.v_variablename' and 'p.p_pointername'.
- Labels: The labels need to be referenced in lowercase when using the in-line ASM. When you reference a label, you must put the prefix 'l_' before the name.
If the label is defined in a procedure, then its prefix is 'll_procedurename_', in lowercase.
When you reference a module item, you must put the prefix 'module_name.l_' all in lowercase before the item.
If the label is defined in a procedure inside a module, then its prefix is 'module_name.ll_procedurename_', in lowercase.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

Demivec wrote: Wed Jun 07, 2023 2:15 am The same as described in the help file under 'Inline x86 ASM'.
well that explains why I couldn't find it - I was searching for "C" in help, not asm.

thanks.
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedding C code

Post by idle »

inline c is useful but it's limited depending upon what your trying to do

take a look at the Raylib4 example in pbcex for instance, it facilitates you to use the library without importing the whole lib

viewtopic.php?t=79332

if you try to embed any c that has #include in it, pb adds it to the main {} which will lead to errors
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: embedding C code

Post by mk-soft »

Global vars was change from v_xxx to g_xxx

Code: Select all

Global iVal.i
Define lVal.l

!g_ival = 100;
!v_lval = 200;

Debug iVal
Debug lVal

Procedure foo(p1, p2)
  Protected r1
  ;r1 = p1 + p2
  !v_r1 = v_p1 + v_p2;
  ProcedureReturn r1
EndProcedure

iVal = foo(1, 2)
Debug iVal
Best way is is show "--commented" c-code
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: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedding C code

Post by idle »

mk-soft wrote: Wed Jun 07, 2023 8:16 am Global vars was change from v_xxx to g_xxx

Code: Select all

Global iVal.i
Define lVal.l

!g_ival = 100;
!v_lval = 200;

Debug iVal
Debug lVal

Procedure foo(p1, p2)
  Protected r1
  ;r1 = p1 + p2
  !v_r1 = v_p1 + v_p2;
  ProcedureReturn r1
EndProcedure

iVal = foo(1, 2)
Debug iVal
Best way is is show "--commented" c-code
that will make things a bit harder, I was quite happily abusing it to get unsigned types and now it probably won't work.
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: embedding C code

Post by mk-soft »

Type conversion from signed to unsigned is also possible.

Code: Select all

Global iVal.i
Procedure foo(p1.l, p2.l)
  Protected r1
  ;r1 = p1 + p2
  !v_r1 = (unsigned long)((unsigned long)v_p1 + (unsigned long)v_p2);
  ProcedureReturn r1
EndProcedure

iVal = foo(1, -2)
Debug iVal
Or misunderstood what you mean
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: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedding C code

Post by idle »

That will work to, I was redefining a variable so it became unsigned and I haven't checked to see if it still works since the bug fix was done. 6.02
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

or just simply...

Code: Select all

t=-3 
! v_t = (unsigned int) v_t;
seems to work nicely...
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedding C code

Post by idle »

it still works :D

Code: Select all

Global a.l 
Global b.l 
!unsigned long g_a; 
a = -1 
b = -1 

Debug a 
Debug b 
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

Even simpler! how the obvious escapes us...

Of course,you could keep it one line to make it a little more 'together'

Code: Select all

Global a.l  : !unsigned long g_a; 
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

How do you access global variables declared inside a module?

This works as expected.

Code: Select all

Global myval1=4096
DeclareModule abc
  Declare test()
EndDeclareModule
Module abc
  Procedure test()
    ! int test1 = g_myval1;
  EndProcedure
EndModule
abc::test()
But I can't access a global var in a module...

Code: Select all

DeclareModule xyz
  Declare test()
EndDeclareModule
Module xyz
  Global myval2=1024
  Procedure test()
    ! int test2 = g_myval2; 
  EndProcedure
EndModule
xyz::test()
Somewhat related, this shouldn't work, right?

Code: Select all

Global myval1 = 2048
DeclareModule xyz
  Declare test()
EndDeclareModule
Module xyz
  Procedure test()
    Protected size
    !v_size = g_myval1;
    Debug size
  EndProcedure
EndModule
xyz::test()
Or this:

Code: Select all

Define myval1 = 2048
DeclareModule xyz
  Declare test()
EndDeclareModule
Module xyz
  Procedure test()
    Protected size
    !v_size = v_myval1;
    Debug size
  EndProcedure
EndModule
xyz::test()
Last edited by jassing on Thu Jun 08, 2023 4:29 am, edited 1 time in total.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedding C code

Post by jassing »

Can you call a function defined in C directly from PB?
or
can you call a PB procedure from w/in C? (Declare with procedurec)?
Post Reply