Tuesday, June 5, 2012

MinGW can't find my libraries

 How to command MinGW using hbmk2 to read my own libraries during compilation of a Harbour source code? Unlike BCC, MinGW is always playing "hard-to-get" to newbies like me. So I made an experiment I would like to share with you. I created a library known as "myLib". With BCC the compiled file name will always be: myLib.LIB; whereas, with MinGW, a prefix "lib" is added to the file name, hence the resulting lib file would look like this:
"libmyLib.a". Note that the suffix is ".a", not ".lib".

1) First Step: Let's create a library.

////////////////////////////////// A program that returns the current path.
/////////////////////////////////// Usage: ? GetPath() --> D:\dev\lib
/////////////////////////////////// File name: trylib.prg
 FUNCTION GetPath()
  RETURN (DiskName()+":\"+CurDir())


2) Second Step: Let's compile our .prg named trylib.prg for us to produce libmyLib.a. In order to prepare our myLib library to contain more functions in the future we need to create an .hbp (Harbour project file. In XBase++, it is termed as .xpj file).

##  myLib.hbp - hbp filename.

# this .hbp file contains only one .prg named trylib.prg which contains a GetPath() function.  

# hblib is the hbmk2 option we need to use in creating a library

 -hblib

# for our compiler, let's use the -compiler switch. If you use a batch file, use
#    set HB_COMPILER=mingw

 -compiler=mingw

# switch -o is needed in order to control the name of the resulting output file.
# We want the name: myLib
-omyLib

# let's now include our .prg/s
trylib.prg

# done

3) Third Step: Let's write our main.prg, which will call our myLib library

/////////////////////////////////// A program that calls getpath() from myLib library
///////////////////////////////////              also known as libmyLib.a
/////////////////////////////////// Usage: just type try.exe at the dot prompt
/////////////////////////////////// File name: try.prg
FUNCTION Main()

cls

? getpath() // returns your current path

inkey(0)

RETURN NIL

4) Fourth Step: Let's write now our .hbp file for our project known as try.exe program. Use number sign "#" to make comments.

## try.hbp, the name of our .hbp file.

# for our compiler
-compiler=mingw

# if we want to control which directory hbmk2 has to do its compiling routines
# after your current path, you will find a new path HB\win\mingw
# hb_plat for platform; hb_comp for compiler

-workdir=HB/${hb_plat}/${hb_comp}

# this is key to command mingw to find your own library.
#     In my experience, mingw can't see your lib file even
#    if it is located in the directory where you compiled your source codes.
# use the following hbmk2 switch: an upper case "L" or -L then your path/current directory

-LD:\dev\lib


# note that in my experiment I didn't add the name of my library, just the path where my library stays.
# then I added another hbmk2 switch, -l, where I need to mention already the name of my library
# take note that this time it is small letter "l" or -l.

-lmyLib


# minGW created libmyLib.a, but I need to write only: -lmyLib


# the name of my output: try.exe, I need the switch -o
-otry


5) Fifth Step: We're almost done, but to make things easier and fast, let's create a batch file we call s.bat (s because I'm left-handed and I want to just easily press s on my keyboard for a trial-and-error compilation I am doing many times in the course of creating a software).

rem batch file name: s.bat
rem I want to set again my compiler, because I want to make sure. To avoid redundancy,
rem you may delete the one written in our .hbp, namely: -compiler=mingw

cls

set HB_COMPILER=mingw

hbmk2 myLib.hbp

rem after compiling our myLib library, we may delete already the above command

rem the added command "1>log.txt 2>&1 is for our log file necessary in tracing errors.

hbmk2 try.hbp 1>log.txt 2>&1

6. Sixth Step. At the command prompt type "dir" to see: try.exe and libmyLib.a. If you'll find them, it means you succeeded. Now is the time to type "try" to see it minGW finds your library. If you are successful you'll find something like this on your screen depending of course on the name of your path:

D> D>dev\myLib

Having the above on your screen, it means the function getpath() in our myLib library was successfully called by try.exe.

Thanks for the patience. Happy Harbour-ing!

No comments:

Post a Comment