Making a library of C code, and Including C code in Harbour wrapper

173 views
Skip to first unread message

Geoffrey Keenan

unread,
Sep 18, 2025, 1:54:46 AM (4 days ago) Sep 18
to Harbour Users
I downloaded the following C file from one of the AI sites recently and have tried to firstly convert it into a library using HBMK2.exe. That seems to build a library file but when I include this lib file in a Harbour app I get compile errors using Borland compiler.
#include <stdio.h>
#include <stdint.h>

// Convert 4-byte Microsoft Binary Format (MBF32) to IEEE float
float mbf32_to_float(uint8_t mbf[4]) {
    if (mbf[0] == 0) return 0.0f; // Zero

    uint8_t exp = mbf[0];
    uint32_t mantissa = (mbf[1]) | (mbf[2] << 8) | ((mbf[3] & 0x7F) << 16);
    int sign = (mbf[3] & 0x80) ? 1 : 0;

    // Adjust exponent: MBF bias=2, IEEE bias=127
    int ieee_exp = exp - 2 + 127;

    // Build IEEE float (sign + exp + mantissa)
    uint32_t ieee_bits = (sign ? 0x80000000 : 0) |
                         ((uint32_t)ieee_exp << 23) |
                         (mantissa & 0x7FFFFF);

    float result;
    *((uint32_t*)&result) = ieee_bits;
    return result;
}

// Convert IEEE float to MBF32 (4 bytes)
void float_to_mbf32(float value, uint8_t mbf[4]) {
    if (value == 0.0f) {
        mbf[0] = mbf[1] = mbf[2] = mbf[3] = 0;
        return;
    }

    uint32_t ieee_bits = *((uint32_t*)&value);
    int sign = (ieee_bits & 0x80000000) ? 1 : 0;
    int ieee_exp = (ieee_bits >> 23) & 0xFF;
    uint32_t mantissa = ieee_bits & 0x7FFFFF;

    // Convert exponent: IEEE bias=127 → MBF bias=2
    int mbf_exp = ieee_exp - 127 + 2;

    // Pack MBF
    mbf[0] = (uint8_t)mbf_exp;
    mbf[1] = (mantissa & 0xFF);
    mbf[2] = (mantissa >> 8) & 0xFF;
    mbf[3] = ((mantissa >> 16) & 0x7F) | (sign ? 0x80 : 0);
}
My second question is how to include the C code directly within the harbour app. I have used the commands #pragma BEGINDUMP & #pragma ENDDUMP to envelope the C code but I get a different set of compile errors, the first being "C:/hb32/include/hbdefs.h:1617:20: error: two or more data types in declaration specifiers
    #define HARBOUR void" plus other errors.
Firstly is the C code workable? or does it have problems? 
Thanks


      

carloskds

unread,
Sep 18, 2025, 11:57:00 AM (4 days ago) Sep 18
to Harbour Users
compilers: harbour and bcc 

filename :test.prg
  for build example:
      hbmk2 test
  for build lib: (create the lib: my lib.lib)
      hbmk2 test -DBUILDLIB -hblib -omylib

/*----------------------------------------------*/
#ifndef BUILDLIB
procedure main()
 ? MySUMC( 12.5, 10.75 )
return
#endif
/*----------------------------------------------*/
#pragma BEGINDUMP
#include <windows.h>
#include "hbapip.h".   

double mySum( double dP1, double dP2  );
/*harbour function*/
HB_FUNC( MYSUMC )
{
 hb_retnd( mySum( hb_parn(1), hb_parn(2) ) )
}
/*c function*/
double mySum( double dP1, double dP2  )
{
 return dP1 + dP2;
}

#pragma ENDDUMP
/*----------------------------------------------*/
//EOF
/*----------------------------------------------*/

carloskds

unread,
Sep 18, 2025, 11:58:59 AM (4 days ago) Sep 18
to Harbour Users
harbour need a wrapper function HB_FUNC( UPPERNAME  ).

carloskds

unread,
Sep 18, 2025, 12:01:51 PM (4 days ago) Sep 18
to Harbour Users
#include "hbapi.h" //harbour definition types and functions.
hb_par??? -> get parameters
hb_ret??? -> return parameters

Geoffrey Keenan

unread,
Sep 18, 2025, 6:50:55 PM (4 days ago) Sep 18
to harbou...@googlegroups.com
Thanks for that Carloskds, I have seen similar simple examples before, and I have previously coded them in and tested them. But this is a more complicated code example. 

Firstly I had put #include "hbapi.h" at top of my Harbour code. 

But the C code I included includes the words 'void' and 'float' imeadiately before the function name - which I understand are C expressions - so how do I handle them in such use?  I have tried several variations without success. For example when using the PRAGMA method do I include everything within the  HB_FUNC(   .  . .  )  ??

And I have no way to test the C codes included independantly. 

There are two separate problems when I tried to use the C code. Firstly I used the c code just as you described before and it created a file 'libmbf2num.a'  or similar. When I built the Harbour app calling this library function I get compile errors in that the function names in the library file are not seen. 

The second problem I get when including the C code within PRAGMA BEGINDUMP etc I get multiple compile errors, the first of which I quoted verbatim in the original post, and I re-quote it again here :
C:/hb32/include/hbdefs.h:1617:20: error: two or more data types in declaration specifiers  #define HARBOUR void"  But there are many more errors. 
  
I am using the Borland 32 bit compiler in Windows 11now.  I started using this compiler when first using Harbour about 10 years ago on Win 7 and have stayed with it as most of my work are relatively simple examples.   

Geoffrey
--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-users/bdb4d6bc-0c7a-4097-8d22-b408127a7fcdn%40googlegroups.com.


-- 
Geoffrey Keenan

marcos...@gmail.com

unread,
Sep 21, 2025, 9:32:10 PM (14 hours ago) Sep 21
to Harbour Users
Hello
Geoffrey Keenan,

The provided code is functionally correct but exhibits compatibility issues with Borland C++ 5.8. When compiled with GNU GCC, it builds without errors.

Attached are the following files:

test_gnu_gcc_mbf32_ieee.c: Original code from the message with main function for testing

Convert_MBF32_to_IEEE_754.c: Modified code compatible with Borland C++ 5.8

Convert_MBF32_to_IEEE_754.prg: Harbour program with embedded C code for conversion "Convert 4-byte Microsoft Binary Format (MBF32) to IEEE float"

The C program was compiled using Code::Blocks 25.03 with gcc-14.2.0.

Execution outputs from both C and Harbour programs:

C Program Output (Convert_MBF32_to_IEEE_754.c):

MBF32 input (bytes shown in the same order you gave): CD CC 8C 3F
Converted IEEE float (decimal): 1.10000002384186
Converted IEEE bytes (MSB first): 3F 8C CC CD
Expected IEEE (MSB-first) for MBF CD CC 8C 3F : 3F 8C CC CD
Process returned 0 (0x0) execution time : 0.012 s
Press any key to continue.


Harbour Program Output (Convert_MBF32_to_IEEE_754.prg):

=== MBF32 -> IEEE-754 test ===
Input MBF32 bytes (hex): CD CC 8C 3F
Converted IEEE float (decimal): 1.10
Converted IEEE bytes (MSB first): 3F 8C CC CD
Expected IEEE (MSB first): 3F 8C CC CD
Press any key to continue...

Original C Code Compiled with GNU GCC (test_gnu_gcc_mbf32_ieee.c):

MBF32 to IEEE Float Converter Test
==================================

Test 1: Zero value
MBF32 representation: 00 00 00 00
Converted IEEE float: 0.000000

Test 2: Positive value (1.0)
MBF32 representation: 81 00 00 00
Converted IEEE float: 170141183460469231731687303715884105728.000000

Test 3: Negative value (-2.0)
MBF32 representation: 82 00 00 80
Converted IEEE float: -inf

Test 4: Convert IEEE float to MBF32 (3.14159)
Original IEEE float: 3.141590
MBF32 representation: 03 D0 0F 49

Test 5: Round-trip conversion (2.71828)
MBF32 representation: 03 4D F8 2D
Original: 2.718280, Converted: 2.718280

Test 6: Very small number (1.234e-10)
MBF32 representation: E1 03 AE 07
Original: 1.234000e-10, Converted: -1.234000e-10

Test 7: Large number (1.234e+10)
MBF32 representation: 23 5D E1 37
Original: 1.234000e+10, Converted: 1.234000e+10

All tests completed successfully!

Process returned 0 (0x0) execution time : 0.007 s
Press any key to continue.


Best regards,

Marcos Jarrin
test_gnu_gcc_mbf32_ieee.c
Convert_MBF32_to_IEEE_754.c
Convert_MBF32_to_IEEE_754.prg
Reply all
Reply to author
Forward
0 new messages