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?
How do I make a library in FB?
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
AddFive.bi <- contains the declare statement for the function(s) in the library
And the example using the library.
AddFive.bas <- compile with -lib
Code: Select all
function AddFive( a as integer ) as integer
return a+5
end function
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
Code: Select all
#include once "AddFive.bi"
print AddFive(10)
print AddFive(4032)
-
- Posts: 1759
- Joined: May 23, 2007 21:52
- Location: Cut Bank, MT
- Contact:
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
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":
And this would be the .BI file for the same library:
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.
-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"
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
Code: Select all
#Inclib "hwObject"
Type hwObject
Declare Constructor ()
Declare Destructor ()
Declare Sub sayIt()
Declare Property nTimes As uInteger
End Type
Here are some links to the manual which may also be of help:
http://www.freebasic.net/wiki/wikka.php ... cLibraries
http://www.freebasic.net/wiki/wikka.php ... dLibraries
http://www.freebasic.net/wiki/wikka.php ... cLibraries
http://www.freebasic.net/wiki/wikka.php ... dLibraries
-
- Posts: 1759
- Joined: May 23, 2007 21:52
- Location: Cut Bank, MT
- Contact:
How do you make a DLL from a .A file ?
How do you make a DLL from a .A file ?
Re: 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.lassar wrote:How do you make a DLL from a .A file ?