fb 2 c

New to FreeBASIC? Post your questions here.
Post Reply
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

fb 2 c

Post by Luxan »

If I declare a function in FreeBasic, is there a possibility of
calling this from c code.

Just now I'm wondering if this might be possible via a pointer.

Code: Select all

#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;
}

Code: Select all


'  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

dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: fb 2 c

Post by dodicat »

fb code.
add.bas

Code: Select all

#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

 
C code
test.c

Code: Select all

#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.
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: fb 2 c

Post by Luxan »

Thanks dodicat

As I'm using Ubuntu I used these commands to compile :

add.bas , just compiled through Geany

gcc "test.c" -o "test" -L "/home/edward/Downloads/fb/" -l add

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 ?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: fb 2 c

Post by dodicat »

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.
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: fb 2 c

Post by Luxan »

Thanks again dodicat

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 :

Build, Set Build Commands

Compile gcc -Wall -c "%f" `pkg-config --cflags add`
Build gcc -ggdb -Wall -o "%e" "%f" `pkg-config --libs add`
Lint cppcheck --language=c++ --enable=warning,style --template=gcc "%f"



Execute "./%e"

The Build flag -ggdb brings in the commands on the Lint, this may not always be required.
Post Reply