Saturday, June 9, 2012

MingW error: Undefined reference to HB_FUN

Have you noticed that hbmk2 will always report an "undefined reference to HB_FUN_..." error if it cannot find the function you referred to or cannot find the library where the same function stayed? Here's an example of this error, especially so if you are using MingW compiler:

filexxx.c:(data+0x58): undefined reference to 'HB_FUN_FUNCTIONNAME'

BCC is more tolerant than MingW. I tried to compile a libmyLib.a where my functions relied on the codes in hbwin, hbxpp, and hbct libraries of Harbour contrib. You may try this experiment:


////// file mylib.prg
function try1()
  ? win_printergetdefault() // requires hbwin library
return nil



function try2()
   sleep(50) // requires hbxpp library
return nil

function try3()
  ? volserial() // requires hbct library
return nil



Try to compile the above codes using hbmk2, compiler=mingw, if you want to see the above error: undefined reference to HB_FUN...


Not only that, should you succeed by using the

 -l switch or
{mingw}libs=${hb_dir}libs${hb_name}.a  // (this switch worked for me)

in compiling your myLib library. For your next problem will be in the actual "swordfight" of your myLib library, since MingW cannot find easily whatever function you wrote in your myLib library. All you need to do is to hire big "L" switch, then the small "l" switch. The so-called big "L" tells the path, whereas, the lower case "l" tells about the library. Here's how to do it in your .hbp file or at the command line:



-LD:\myDir\myWork
-lmyLib (don't include the prefix (lib) and the file extension name, ".a"


Still these hbmk2 options won't work if you have imported functions from other libraries, f.e., hbwin.


Why? Because you need also to mention in your exe hbp the names of the contril libs where you based your myLib libraries.

I have this experience since I have functions in my XBase++ days which used XBTools III like volserial() which is very important to me. I want to share the following .hbp files, one for the lib, and the other for the exe file.

################## .hbp for the library file, example: myLib Library
## myLib.hbp


-inc
-w3 -es2
-workdir=HB/${hb_comp}
-hbx=blah.hbx        < try it, it's okey
{mingw}libs=${hb_dir}libs${hb_name}.a
-hblib
-info
-omyLib

 ################## .hbp for the exe file, example: try.exe
 ## try.hbp


-w3 -es2
-mt
-trace
-inc
-lhbxpp
-lhbct
-lhbwin
-LD:\dev\2012\myprog
-lmyLib
-info
-otry

Note that I am careful about the listing order of my libraries. From the forum, I learned Victor saying that ordering of libraries is a factor for the compiler.

My other blog on this topic may be of interest to you at My Harbour Notes: MingW cannot find my libraries.



No comments:

Post a Comment