Feature request : Call a function from Backend C in Purebasic

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Feature request : Call a function from Backend C in Purebasic

Post by threedslider »

Hello all,

I want to make a call a function from Backend C in Purebasic, possible ? :)

Example :

Code: Select all

OpenConsole()

!void My_text(char mytext[])
!{
!   printf(mytext);
!}

Prototype My_text1(body.p-ascii)

msg.My_Text1 = GetFunctionBackendC("My_text")

msg("hello world")

Input()

CloseConsole()
That would be very nice is Fred integrates this !! Thanks !!! :shock: :D :mrgreen:
User avatar
idle
Always Here
Always Here
Posts: 5887
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Feature request : Call a function from Backend C in Purebasic

Post by idle »

You can do this via prototype and assign the c function to the prototype in inline c
Then call it.

Most of the issues of working with c could be resolved if #include were moved out of main and you had the c system headers matching the gcc tool chain

Then you could import c libs and only wrap the functions you need
User avatar
idle
Always Here
Always Here
Posts: 5887
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Feature request : Call a function from Backend C in Purebasic

Post by idle »

example

Code: Select all

PrototypeC.q pFastHash64(*buf,len.i,seed.q)  ;prototype function  
Global FastHash64.pFastHash64                ;make global of prototype   
;prefix local parameters p_ pointer v_ variables 
!integer fasthash64c(integer p_buf,integer v_len,integer v_seed) {
! typedef unsigned long long uint64_t; 
! #define mix(h) ({				      	  \
! 			(h) ^= (h) >> 23;		          \
! 			(h) *= 0x2127599bf4325c37ULL;	\
! 			(h) ^= (h) >> 47; })
! 	const uint64_t m = 0x880355f21e6d1965ULL;
! 	const uint64_t *pos = (const uint64_t *)p_buf;
! 	const uint64_t *end = pos + (v_len / 8);
! 	const unsigned char *pos2;
! 	uint64_t h = v_seed ^ (v_len * m);
! 	uint64_t v;
!  uint64_t result; 
! 	while (pos != end) {
! 		v  = *pos++;
! 		h ^= mix(v);
! 		h *= m;
! 	}
! 	pos2 = (const unsigned char*)pos;
! 	v = 0;
! 	switch (v_len & 7) {
! 	case 7: v ^= (uint64_t)pos2[6] << 48;
! 	case 6: v ^= (uint64_t)pos2[5] << 40;
! 	case 5: v ^= (uint64_t)pos2[4] << 32;
! 	case 4: v ^= (uint64_t)pos2[3] << 24;
! 	case 3: v ^= (uint64_t)pos2[2] << 16;
! 	case 2: v ^= (uint64_t)pos2[1] << 8;
! 	case 1: v ^= (uint64_t)pos2[0];
! 		h ^= mix(v);
! 		h *= m;
! 	}
! 	return mix(h);
! } 

;assign c function address to prototype 
!g_fasthash64 = &fasthash64c;  
 
 
 Global str.s = "Calling an inline c function" 
 
Debug FastHash64(@str,StringByteLength(str),0) ;call the proceedure 

;or just call it 
Global hash,len 
len = StringByteLength(str)
!g_hash = fasthash64c(g_str,g_len,0); 
Debug hash 
 
 
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Feature request : Call a function from Backend C in Purebasic

Post by Mijikai »

idle wrote: Thu Aug 28, 2025 1:57 am Most of the issues of working with c could be resolved if #include were moved out of main and you had the c system headers matching the gcc tool chain
Then you could import c libs and only wrap the functions you need
+1
Post Reply