Adding a C function

Adding a C function

Postby James Bott » Thu Sep 10, 2009 8:49 pm

I have to admit I don't really understand how the whole PRG to C to EXE system works. And I am not a C programmer.

I have a C program with a function, lets call it "Myfunction" in it, defined like this:

CLIPPER MYFUNCTION( PARAMS )
...

This is compiled using BCC then linked with the PRGs.

But the linker errors out with:

undefined external '_HB_FUN_MYFUNCTION'

I presume this is because the function call to MYFUNCTION in the PRG is being translated by the preprocessor to _HB_FUN_MYFUNCTION. If I change the name of the C function to _HB_FUN_MYFUNCTION then I get a bunch more errors.

OK, so can someone tell me the secret to getting this working?

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Adding a C function

Postby Enrico Maria Giordano » Thu Sep 10, 2009 8:55 pm

You have to use

Code: Select all  Expand view
HB_FUNC( MYFUNCTION )


in [x]Harbour.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Adding a C function

Postby James Bott » Thu Sep 10, 2009 9:22 pm

Enrico,

I'm sorry I still don't understand. Do you mean in the C program?

Change this:
CLIPPER MYFUNCTION( PARAMS )

To this?
HB_FUNC( MYFUNCTION )

Or, ?

I am using xHarbour.

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Adding a C function

Postby James Bott » Thu Sep 10, 2009 9:44 pm

OK, I changed it to:

HB_FUNC( MYFUNCTION ( PARAMS ) )

Then I get these compiler errors:

Error E2147 source\inifunc.c 24: 'MYFUNCTION' cannot start a parameter declaration
Error E2293 source\inifunc.c 24: ) expected

This is the way the FWH\tools\clp2harb.exe program seems to be doing it.

I still must be doing something wrong.

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Adding a C function

Postby James Bott » Thu Sep 10, 2009 10:00 pm

I now see that clp2harb.exe is changing it like this:

HB_FUNC( MYFUNCTION )

So I tried that and I get these errors:

Warning W8070 source\inifunc.c 18: Function should return a value in function HB_FUNC
Warning W8057 source\inifunc.c 18: Parameter 'MYFUNCTION' is never used in function HB_FUNC
Error E2293 source\inifunc.c 54: ) expected

Here is the complete code for my test function, MYFUNCTION.

Code: Select all  Expand view
HB_FUNC( MYFUNCTION )
{
   _retni( GetProfileInt( _parc( 1 ),
                          _parc( 2 ),      
                          _parni( 3 ) ) );
}

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Adding a C function

Postby Antonio Linares » Thu Sep 10, 2009 10:17 pm

James,

Please change it this way:
Code: Select all  Expand view

HB_FUNC( MYFUNCTION )
{
   hb_retni( GetProfileInt( hb_parc( 1 ), hb_parc( 2 ), hb_parni( 3 ) ) );
}
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42081
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Adding a C function

Postby James Bott » Thu Sep 10, 2009 10:33 pm

Antonio,

I tried that already and I get this error:

Warning W8065 source\inifunc.c 20: Call to function 'GetProfileInt' with no prototype in function HB_FUN_MYFUNCTION

James


Code: Select all  Expand view
#include "hbapifs.h"

HB_FUNC( MYFUNCTION )
{
   hb_retni( GetProfileInt( hb_parc( 1 ),
                          hb_parc( 2 ),
                          hb_parni( 3 ) ) );
}
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Adding a C function

Postby Antonio Linares » Thu Sep 10, 2009 10:38 pm

James,

You have to add:

#include <windows.h>

so Windows API functions are properly identified.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42081
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Adding a C function

Postby James Bott » Thu Sep 10, 2009 10:41 pm

Antonio,

Yea! That did it. Thanks!

I guess I shot myself in the foot. I thought I would start with a known good C program so I selected one from the FWH\source directory. Now it seems that these are not compiled as-is but they have to go through some preprocessing before they can be compiled. Is that true?

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Adding a C function

Postby Antonio Linares » Thu Sep 10, 2009 10:49 pm

James,

In FWH 9.08 we have removed CLIPPER ... and PARAMS from all C modules, as we only focus on Harbour and xHarbour now.

If you use FWH 9.08 then there is no need to use clp2harb.exe never more to compile FWH C modules :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42081
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Adding a C function

Postby James Bott » Thu Sep 10, 2009 11:57 pm

I have another question.

Is this a complete function definition?

void pascal DelResource( HANDLE hRes );

The compiler doesn't error, but the linker gives an "unresolved external DelResource" error.

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Adding a C function

Postby Antonio Linares » Fri Sep 11, 2009 12:07 am

James,

void pascal DelResource( HANDLE hRes );


Thats what is called a function "prototype": It instructs the C compiler how to manage that function, but it does not implement its code.

The compiler doesn't error, but the linker gives an "unresolved external DelResource" error.


FiveWin provides such function, so if you link with FiveHC.lib then the linker should find it.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42081
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Adding a C function

Postby Antonio Linares » Fri Sep 11, 2009 12:14 am

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42081
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 99 guests