C to FreeBasic function

DOS specific questions.
Cpcdos
Posts: 192
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

C to FreeBasic function

Postby Cpcdos » Mar 08, 2016 9:13

Hi,
I would like to execute a FreeBasic function from my C code. (GCC 5.1 for dos --> C / C++)
My C code produce a .A library that I use into my freebasic code.
Example, FreeBasic code :

Code: Select all

Declare function abc(a as integer, b as integer) as integer
function abc(a as integer, b as integer) as integer
    dim result as integer = a + b
    abc = result
end function


For my C code I've tested more solutions :
  • Method 1

    Code: Select all

    extern int abc(int a, int b);
    int result;
    result = abc(123, 150);

    When I compile, freebasic compiler return "undefined reference to 'abc(int, int)'"
    I tested with ABC(int, int) , _abc(int, int) or _ABC(int, int) .. Same problem
     
  • Method 2
    When I compile my freebasic code, my abc() function is called "_ABC" in asm code
    so, in my C code i test :

    Code: Select all

    asm("call _ABC");
    this works but with random value for a and b variables

    So i test this

    Code: Select all

    asm("push 123"); // a
    asm("push 150"); // b
    asm("call _ABC");
    crash... SIGSEGV signal

Someone have a solution ? :(
Thank you
fxm
Posts: 7359
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: C to FreeBasic function

Postby fxm » Mar 08, 2016 10:20

Perhaps:
function abc(a as integer, b as integer) as integer export
MrSwiss
Posts: 2021
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: C to FreeBasic function

Postby MrSwiss » Mar 08, 2016 10:26

@Cpcdos,

I'd first check the Int Type in C. I suppose on DOS it's probably 16bit.
Therefore, in FB, it would be a Short = 16bit = 2xByte = Word (mainly used in ASM).

The C and FB Types MUST match each other (same Size, at least + with/without Sign bit).

FB-DOS = 32bit. FB x86/32 so, Integer and Long are both 4 Bytes long.
Cpcdos
Posts: 192
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: C to FreeBasic function

Postby Cpcdos » Mar 08, 2016 11:15

@MzSwiss Yes, you have reason I've already encounter this problem with my previous project (PDS 7.1 16bits and gcc 4.7 32bits) but today I use GCC 5.1, it produces 32bits code (I use HXDPMI by Japhet)

@fmx Thank you very much, the key word "export" approached me with the "cdecl" and "alias" in freebasic manual, so I've tested :

Code: Select all

Declare function abc cdecl alias "abc" (a as integer, b as integer) as integer
function abc cdecl alias "abc"(a as integer, b as integer) as integer export
    dim result as integer = a + b
    abc = result
end function


And C code :

Code: Select all

extern "C" int abc(int a, int b);
int result;
result = abc(123, 150);

And this works :D

Thank you :)
dkl
Site Admin
Posts: 3166
Joined: Jul 28, 2005 14:45
Location: Germany

Re: C to FreeBasic function

Postby dkl » Mar 09, 2016 19:26

By the way, you can also use Extern "C" ... End Extern to get C calling convention and name mangling in FreeBASIC. It's a shortcut for the cdecl/alias combination.

Code: Select all

extern "C"
   function abc(a as integer, b as integer) as integer
       dim result as integer = a + b
       abc = result
   end function
end extern


Export is for Windows DLLs only.

Return to “DOS”

Who is online

Users browsing this forum: No registered users and 1 guest