embedding C code
embedding C code
With the C backend, is it possible to embed C code like you can with ASM?
Re: embedding C code
OK, how do I pass a variable between PB and C?
Re: embedding C code
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.
Re: embedding C code
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
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
Re: embedding C code
Global vars was change from v_xxx to g_xxx
Best way is is show "--commented" c-code
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
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: embedding 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.mk-soft wrote: ↑Wed Jun 07, 2023 8:16 am Global vars was change from v_xxx to g_xxx
Best way is is show "--commented" c-codeCode: 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
Re: embedding C code
Type conversion from signed to unsigned is also possible.
Or misunderstood what you mean
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
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: embedding C code
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
Re: embedding C code
or just simply...
seems to work nicely...
Code: Select all
t=-3
! v_t = (unsigned int) v_t;
Re: embedding C code
it still works

Code: Select all
Global a.l
Global b.l
!unsigned long g_a;
a = -1
b = -1
Debug a
Debug b
Re: embedding C code
Even simpler! how the obvious escapes us...
Of course,you could keep it one line to make it a little more 'together'
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;
Re: embedding C code
How do you access global variables declared inside a module?
This works as expected.
But I can't access a global var in a module...
Somewhat related, this shouldn't work, right?
Or this:
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()
Code: Select all
DeclareModule xyz
Declare test()
EndDeclareModule
Module xyz
Global myval2=1024
Procedure test()
! int test2 = g_myval2;
EndProcedure
EndModule
xyz::test()
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()
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.
Re: embedding C code
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)?
or
can you call a PB procedure from w/in C? (Declare with procedurec)?