Is there a way to incorporate C code into a FreeBasic program as a callable function in the same way that one can import ASM code? I've read about calling functions in C libraries, but my needs are a little different.
Essentially I would like to adapt the Decode function of LZHUF.C. I would like to call it from FreeBasic as Decode( infile, outfile ). I don't need the Encode part of LZHUF.C. All error checking would be done in the external FreeBasic code, so that part of the C code could be omitted, too.
This is the original code and DOS executable:
http://www.users.on.net/~fzabkar/temp/LZHuf/LZHUF.C
http://www.users.on.net/~fzabkar/temp/LZHuf/LZHUF.EXE
I have stripped out the stuff that I don't need:
http://www.users.on.net/~fzabkar/temp/L ... f_Decode.c
I presume that the "void Decode(void)" section needs to be modified. Note the references to Turbo-C in the source code.
Here are two sample files:
http://www.users.on.net/~fzabkar/temp/L ... 5_DCB0.bin (compressed infile)
http://www.users.on.net/~fzabkar/temp/L ... 5_DCB0.bin (decompressed outfile)
BTW, I'm not a programmer. I've written some Basic code, but not C.
Thanks for any assistance.
Importing C code into a FreeBasic program
Re: Importing C code into a FreeBasic program
fbc works well with C compiled code. So if you have the original .c file, and can compile it for the target you are interested (win/lin/dos) then it is actually pretty easy to get the compiled code in to your fbc program.
1) write declarations in FreeBASIC (declare ...)
2) with gcc compile the the '.c' c source file for target (win/lin/dos) to a '.o' object file
3) make the '.o' file in to a static library, or link directly in to your program. On fbc command line, you can give file names of FB source code or object files '.o'
OK, I know, this is a partial answer only. You are going to have to fill in some gaps here on your own.
1) write declarations in FreeBASIC (declare ...)
2) with gcc compile the the '.c' c source file for target (win/lin/dos) to a '.o' object file
3) make the '.o' file in to a static library, or link directly in to your program. On fbc command line, you can give file names of FB source code or object files '.o'
OK, I know, this is a partial answer only. You are going to have to fill in some gaps here on your own.
Re: Importing C code into a FreeBasic program
You'll also have to use the EXTERN keyword in FB to gain access to the global symbols defined in the C module. Either that, or rewrite the C functions to return information to you, rather than rely on global state. Anywhere where C defines a char array, such as text_buf[], that's the equivalent of a ZSTRING PTR in FB.
This C routine expects to work with a C standard library FILE structure. To get that you'll have to use the C runtime fopen() function rather than FB's built-in file handling routines, although if you can get a file handle from FB, you can get a FILE * structure from that with the runtime call fdopen(). These are accessible in FB when including "crt.bi" in your FB code.
EDIT:
Another possibility is to translate the entire C code you're looking at into FB. It's a fairly easy 1:1 translation. Just remember that C arrays are defined by the total number of elements, whereas FB defines them based on the upper bound. In other words, the c array "int b[5]" is the same as "dim as long b(4)" in FB (the full dialect).
This C routine expects to work with a C standard library FILE structure. To get that you'll have to use the C runtime fopen() function rather than FB's built-in file handling routines, although if you can get a file handle from FB, you can get a FILE * structure from that with the runtime call fdopen(). These are accessible in FB when including "crt.bi" in your FB code.
EDIT:
Another possibility is to translate the entire C code you're looking at into FB. It's a fairly easy 1:1 translation. Just remember that C arrays are defined by the total number of elements, whereas FB defines them based on the upper bound. In other words, the c array "int b[5]" is the same as "dim as long b(4)" in FB (the full dialect).
Last edited by caseih on Oct 07, 2018 18:48, edited 1 time in total.
Re: Importing C code into a FreeBasic program
This is definitely NOT correct, FB arrays by default start with 0 (same as C)caseih wrote:Just remember that C arrays start at 0 and FB arrays start at 1 ...
unless, explicitly specified as: array(1 to n).
The difference is: in C the number specifies the number of elements, whereas
in FB, you are defining the arrays upper bound:
Code: Select all
Dim As Byte byte_arr(5) ' defines actually ...
Dim As Byte byte_arr(0 to 5) ' 6 elements
Re: Importing C code into a FreeBasic program
I stand corrected!