Importing C code into a FreeBasic program

New to FreeBASIC? Post your questions here.
fzabkar
Posts: 1
Joined: Sep 29, 2018 2:52

Importing C code into a FreeBasic program

Postby fzabkar » Sep 29, 2018 3:00

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/LZHuf/LZHuf_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/LZHuf/revBlk05_DCB0.bin (compressed infile)
http://www.users.on.net/~fzabkar/temp/LZHuf/decBlk05_DCB0.bin (decompressed outfile)

BTW, I'm not a programmer. I've written some Basic code, but not C.

Thanks for any assistance.
coderJeff
Site Admin
Posts: 2717
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Importing C code into a FreeBasic program

Postby coderJeff » Oct 07, 2018 13:06

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.
caseih
Posts: 1300
Joined: Feb 26, 2007 5:32

Re: Importing C code into a FreeBasic program

Postby caseih » Oct 07, 2018 15:46

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).
Last edited by caseih on Oct 07, 2018 18:48, edited 1 time in total.
MrSwiss
Posts: 2817
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Importing C code into a FreeBasic program

Postby MrSwiss » Oct 07, 2018 17:41

caseih wrote:Just remember that C arrays start at 0 and FB arrays start at 1 ...
This is definitely NOT correct, FB arrays by default start with 0 (same as C)
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
caseih
Posts: 1300
Joined: Feb 26, 2007 5:32

Re: Importing C code into a FreeBasic program

Postby caseih » Oct 07, 2018 18:22

I stand corrected!

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests