PureBasic uses its own internal format for libraries, and allows you to create your own libraries in ASM or C. So, if you want to create PureBaisc libraries, first you need your code translated either to ASM or C.
PureBasic creates the file 'PureBasic.asm' when called with the /COMMENTED option. So, since we already have the ASM source for our library, we only need to make some changes to it so we can use it as source for a PureBasic library. This is what TailBite does for you: call the PB compiler, split the 'PureBasic.asm' file into several ASM files (roughly, one for each function), compile the ASM files into OBJ files, join those OBJ files into a LIB file, create the DESC file (with the description of the library) and call Library Maker (in the PureBasic/Library SDK/ folder), which actually makes the final library and installs it in the PureBasic/PureLibraries/UserLibraries/ folder.
The TailBite Installer adds TailBite to the tools menu. Once you have your code ready, you only have to select TailBite from the tools menu.
So, how does your code have to look like so TailBite can make a PureBasic library out of it? Just follow some simple rules:
TailBite assumes any ProcedureDLL
to be an exported function, that is, a function that you will be able to call from PureBasic, as any other PureBasic function:
ProcedureDLL MyDiv(a, b) ProcedureReturn a/b EndProcedure
You can use normal Procedure
's for internal library use. Executables that use your library will only include the functions that are used in the code, and only the internal functions used by those functions. To achieve this, the 'PureBasic.asm' file has to be splitted and compiled into several OBJ files:
Procedure LocalFindString(a$, b$) ProcedureReturn FindString(a$, b$, 1) EndProcedure ProcedureDLL.s MyReplaceString(a$, b$, c$) chi = LocalFindString(a$, b$) result$ = Left(a$, chi)+c$+Right(a$, Len(a$)-Len(b$)) ProcedureReturn result$ EndProcedure
You can make your function accept variable arguments. To do so, you must include two or more ProcedureDLL
's with the same name and an index number:
ProcedureDLL MyFunction(arg1, arg2) result = Pow(arg1, arg2) ProcedureReturn result EndProcedure ProcedureDLL MyFunction2(arg1, arg2, arg3) result = Pow(arg1, arg2)/arg3 ProcedureReturn result EndProcedure
To be considered as different versions of the same function with different argument number, the arguments names and types must coincide between them; otherwise, both functions will be considered different functions and processed that way.
After the library is made, and the compiler restarted from the PureBasic editor, if you type MyFunction(
in the PureBasic editor, the status window will show:
MyFunction (arg1, arg2 [, arg3])
And you'll be able to call the function with two or three arguments. You can make as many versions as you want, with a different number of arguments, as long as you follow the mentioned rules.
To append a short help line to your function, just add a comment after the ProcedureDLL
header, like in the example:
ProcedureDLL MyDiv(a, b) ; This is my help line ProcedureReturn a/b EndProcedure
And, after the library is made, and the compiler restarted from the PureBasic editor, if you type MyFunction(
in the PureBasic editor, the status window will show:
MyFunction (arg1, arg2) - This is my help line
Use it only in the main function, not in debug functions, nor in CPU specific functions. If you have a function with different argument number versions, put the help comment in the ProcedureDLL
header with less arguments. Any other comments in the code are ignored by TailBite.
If you need to do initialization operations at program start whenever your library is used, or clean/shutdown operations, you must use two special suffixes in two functions: _Init
and _End
. These functions can take no arguments, nor can they any values. Use only one _Init
function and/or one _End
function per library:
ProcedureDLL MyLib_Init() ; Do some initializing stuff, allocate memory, etc. EndProcedure ProcedureDLL MyLib_End() ; Do some cleaning, free memory, unload third party DLLs, etc. EndProcedure
You can compile your library from a .DESC file rather than from PureBasic source. When you use TailBite with a .Desc file, TailBite will search for ASM files in the same directory or subdirectories and create the library from them.
You can also create a resident file with TailBite: if TailBite doesn't find any procedure in the code, it'll assume that you want to create a resident file. Remember that in a resident source file there can only be constants, interfaces and structure definitions.