How do I make a library in FB?

New to FreeBASIC? Post your questions here.
Post Reply
Mentat
Posts: 332
Joined: Oct 27, 2007 15:23
Location: NC, US
Contact:

How do I make a library in FB?

Post by Mentat »

For example, a library with the function that returns the number five.
And do I have to include it differently than a header file?
And is the a performance and/or coding difference between using a header and a library?
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Post by sir_mud »

well, there are two types of libraries to think about, static and dynamic. static would be compiled into your program and would not have to be distrubuted with the final product where as a dynamic library (dll on windows, so on linux) would need to be distributed with the final product. There really shouldn't be any difference in performance, and the coding differences are small. Here is a small example of how to use a static library:
AddFive.bas <- compile with -lib

Code: Select all

function AddFive( a as integer ) as integer
   return a+5
end function
AddFive.bi <- contains the declare statement for the function(s) in the library

Code: Select all

#inclib "AddFive" 'this line tells the compiler you're using the AddFive library (libAddFive.a)
'Its always a good idea to document what functions in your libraries do, like:
''Function: AddFive
''Adds 5 to an integer
''
''Parameters:
''a - the integer to add 5 to.
''
''Returns:
''Integer containing the sum of a and 5.
''
declare function AddFive( a as integer ) as integer
And the example using the library.

Code: Select all

#include once "AddFive.bi"

print AddFive(10)
print AddFive(4032)

notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

A little more details on static libraries (I had trouble with this at first too):

-Just a regular .BAS file but usually you only have Subs and Functions, or perhaps UDTs. Global variables and module-level code is allowed, but used less. You can also have Constructors and Destructors for when the library is loaded/unloaded.
-Compile with the "-lib" option. The resulting file will be named "libNAME.a", where the original file was called "NAME.bas". You need to move this file to the library directory in your FreeBASIC installation (for example "C:\Program Files\FreeBASIC\lib\win32\") - I usually use a batch file for this, as otherwise it gets tedious when testing a library. For every Sub, Function, UDT, etc. that you use in the library, you need to declare it in a separate file, usually with the same name but ending with .bi (such as "NAME.bi"). This file usually has the line

Code: Select all

#Inclib "NAME"
which includes the library file you created (FBC automatically adds the "lib" and ".a" to the name, searching the library directory for the file).

Thus, for example, this would be "hwObject.bas" which compiled with -lib would become "libhwObject.a":

Code: Select all

Type hwObject
  Public:
    Declare Constructor ()
    Declare Destructor ()
    Declare Sub sayIt()
    Declare Property nTimes As uInteger
  Private:
    _a As uInteger
End Type

Constructor hwObject ()
  '...
End Constructor

Destructor hwObject ()
  '...
End Destructor

Sub hwObject.sayIt ()
  Print "Hello, World!"
  this._a = this._a + 1
End Sub

Property hwObject.nTimes As uInteger
  Return this._a
End Property
And this would be the .BI file for the same library:

Code: Select all

#Inclib "hwObject"

Type hwObject
    Declare Constructor ()
    Declare Destructor ()
    Declare Sub sayIt()
    Declare Property nTimes As uInteger
End Type
Note that any constants or anything else will need to be defined in the .BI file, but Subs/Functions/UDTs only need to be declared (as the actual code defining them is in the pre-compiled library). You also only need to declare Subs/Functions from the library that are for the public to use - you may have Subs/Functions used internally in the library that do not need to be declared in the .BI file.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

Mentat
Posts: 332
Joined: Oct 27, 2007 15:23
Location: NC, US
Contact:

Post by Mentat »

Thanks. How do I make one with FBIDE? Today I updated the path to a more recent compiler (0.18.2), and I'm guessing the -lib goes in the same tab.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

Well, your IDE will have some path options and you'll need to add "-lib" to the path options when you're compiling a library, then remove it when you aren't. FBEdit really makes things easier, it has a drop-down box that lets you choose what you're trying to compile.
lassar
Posts: 306
Joined: Jan 17, 2006 1:35

How do you make a DLL from a .A file ?

Post by lassar »

How do you make a DLL from a .A file ?
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Re: How do you make a DLL from a .A file ?

Post by cha0s »

lassar wrote:How do you make a DLL from a .A file ?
Please only post about a single question in one place. If you don't get a response, feel free to bump the original post.
Post Reply