Reliable method of telling Harbour and xHarbour apart

240 views
Skip to first unread message

Alexandre Cavalcante Alencar

unread,
Feb 23, 2017, 3:30:28 PM2/23/17
to Harbour Users
Hello there,

I am porting some PRG code to Harbour and in my first steps, I stumbled with xHarbour odd behaviors. For some functions, xHarbour and Harbour behavior is different and I need to, based in what compiler was used, produce diferent code.

In Harbour, this worked fine using __HARBOUR__ define, but for xHarbour, using the __XHARBOUR__ define resulted in odd and unexpected results.

How to tell them apart using a reliable (xHarbour-proof) method?

/* 
 * SAMPLE.PRG 
 * 
 */ 

REQUEST HB_GT_WIN 

PROCEDURE MAIN 

#ifdef __XHARBOUR__ 
? "We where build by xHarbour" 
? "DiskReady available" 
#endif /* __XHARBOUR */ 

#ifdef __HARBOUR__ 
? "We where build by Harbour" 
? "DiskReady not available" 
#endif /* __HARBOUR__ */ 

/* end of sample */ 

With Harbour, running harbour sample -p will produce (expected): 

EXTERNAL HB_GT_WIN 

PROCEDURE MAIN 

QOut( "We where build by Harbour" ) 
QOut( "DiskReady not available" ) 

With xHarbour, running harbour sample /p will produce (not expected): 

EXTERNAL HB_GT_WIN 

PROCEDURE MAIN 

QOut( "We where build by xHarbour" ) 
QOut( "DiskReady available" ) 
QOut( "We where build by Harbour" ) 
QOut( "DiskReady not available" ) 

Gale Ford

unread,
Feb 23, 2017, 4:53:21 PM2/23/17
to Harbour Users
Can you use #else


#ifdef __XHARBOUR__ 
  ? "We where build by xHarbour" 
  ? "DiskReady available" 
#else

  ? "We where build by Harbour" 
  ? "DiskReady not available" 
#endif /* __HARBOUR__ */

or how about #ifndef

#ifdef __XHARBOUR__ 
? "We where build by xHarbour" 
? "DiskReady available" 
#endif /* __XHARBOUR */ 

#ifndef __XHARBOUR__
? "We where build by Harbour" 
? "DiskReady not available" 
#endif /* __HARBOUR__ */ 



Klas Engwall

unread,
Feb 23, 2017, 6:12:13 PM2/23/17
to harbou...@googlegroups.com
Hi Alexandre,

> I am porting some PRG code to Harbour and in my first steps, I stumbled
> with xHarbour odd behaviors. For some functions, xHarbour and Harbour
> behavior is different and I need to, based in what compiler was used,
> produce diferent code.
>
> In Harbour, this worked fine using __HARBOUR__ define, but for xHarbour,
> using the __XHARBOUR__ define resulted in odd and unexpected results.
>
> How to tell them apart using a reliable (xHarbour-proof) method?

Not really unexpected :-)

In the beginning, __HARBOUR__ was #define'd to distinguish between
Harbour and Clipper. Then, in the xHarbour fork, __XHARBOUR__ was
#define'd to distiguish it from Harbour. But there was still need to
distinguish (x)Harbour from Clipper, so the original __HARBOUR__ was
kept alongside __XHARBOUR__

If you never need to worry about Clipper (or any other toolchain),
Gale's method of using a simple #else will do the trick to distinguish
between Harbour and xHarbour. If there is also a Clipper option, you
would have to do something like this:

#if defined( __XHARBOUR__ )
// xHarbour code here
#elif defined( __HARBOUR__ )
// Harbour code here
#else
// Clipper code here
#endif

Regards,
Klas

Klas Engwall

unread,
Feb 23, 2017, 7:30:53 PM2/23/17
to harbou...@googlegroups.com
Hi again,

> #if defined( __XHARBOUR__ )
> // xHarbour code here
> #elif defined( __HARBOUR__ )
> // Harbour code here
> #else
> // Clipper code here
> #endif

... perfect, except that Clipper doesn't understand the #if ... #elif
syntax. Dang :-)

Regards,
Klas

Alexandre Cavalcante Alencar

unread,
Feb 23, 2017, 7:57:03 PM2/23/17
to Harbour Users
Gale, Klas,

The canned aproche worked as expected, and I have not to worry about Clipper (or other XBase++ compilers for the time being). This is needed for some few situations where xHarbour (our current compiler) and Harbour (migrating to) functions differ (or not exist), for now, a lot of small simple condicional branches.

Thank' s for the suggestion.

=== worked ===
#if defined(...)
  ...
#elif defined (...)
  ...
#else
  ...
#endif 
Message has been deleted

carloskds

unread,
Feb 24, 2017, 11:26:31 AM2/24/17
to Harbour Users
I think this: 
both compiler have the constant __HARBOUR__, but only xharbour have the __XHARBOUR__ constant. 

the correct use of condicional compiler is: 
#ifdef __XHARBOUR__ 
 ... only xharbour compile this line. 
#endif 

#ifdef __HARBOUR__ 
   #ifndef __XHARBOUR__ 
      ... this compile only with harbour 
   #else 
      ... this compile only with xharbour 
   #endif 
#endif 

salu2 
carlos vargas. 
Reply all
Reply to author
Forward
0 new messages