#include <stdio.h>
// A normal function with an int parameter
// and void return type
// func_ptr.c
// alternately, a function, with cdecl, from fb ?
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int) = &fun;
/* The above line is equivalent of following two
void (*fun_ptr)(int);
fun_ptr = &fun;
*/
// Invoking fun() using fun_ptr
(*fun_ptr)(10);
return 0;
}
' func_ptr2.bas
' This example uses ProcPtr to demonstrate function pointers
Declare Function Subtract( x As Integer, y As Integer) As Integer
Declare Function Add( x As Integer, y As Integer) As Integer
Dim myFunction As Function( x As Integer, y As Integer) As Integer
' myFunction will now be assigned to Add
myFunction = ProcPtr( Add )
Print myFunction(2, 3)
' myFunction will now be assigned to Subtract. Notice the different output.
myFunction = ProcPtr( Subtract )
Print myFunction(2, 3)
Function Add( x As Integer, y As Integer) As Integer
Return x + y
End Function
Function Subtract( x As Integer, y As Integer) As Integer
Return x - y
End Function
#cmdline "-lib"
Function Add( x As Integer, y As Integer) As Integer
Return x + y
End Function
Function Subtract( x As Integer, y As Integer) As Integer
Return x - y
End Function
#include <stdio.h>
#include <stdlib.h>
extern int ADD(int a,int b);
extern int SUBTRACT(int a,int b);
int (*add_pt)(int,int) = &ADD;
int (*subtract_pt)(int,int) = &SUBTRACT;
int main()
{
int k;
k= ADD(4,5);
printf("%d\n",k);
k=SUBTRACT(1,9);
printf("%d\n",k);
printf("%d\n",(*add_pt)(10,12));
printf("%d\n",(*subtract_pt)(10,12));
system("pause");
}
calling the linker in c
-l add
Command: gcc.exe "C:\ ... test.c" -o "C:\ ... test.exe" -L"C:\PATH TO LIBRARIES" -l add
note
extern keyword is not needed in c, but it acts as a reminder that the functions are in a static lib.
Then ./test from the same folder, to produce this output :
9
-8
22
-2
This is a static library, if Linux has dynamic libraries might these also be linked.
I was hoping to write fb code within a IDE, like Geany, then call that from c code
without generating a static library file from a *.bas file.
I use Geany a lot, however I'm having difficulty linking the add library file when I
attempt to use that to compile the run test.c; any advice ?
If you remember to tag export to each sub/function, and compile to dll, then the c code given will load the dll at compile time, just like the static lib, or you can use loadlibrary from winapi to load the dll at runtime.
Continuing with Ubuntu, appending export to the end of each function in
add.bas, compiling, via Geany, to libadd.a; both files are in the same directory as
test.c .
Then using Geany to compile test.c to test and running; now the correct results appear.
The build commands I used, within Geany, to compile test.c are :