Compiling a library
-
- Posts: 21
- Joined: Mar 31, 2011 17:30
- Location: Rome, Italy
Compiling a library
Could someone tell me how to create a library (".a" extension I guess) from a .bas source?
And after I created it, how can I link it (I'm using FBIDE) in a project so that it can be "seen" by all the project modules?
Thanks in advance!
And after I created it, how can I link it (I'm using FBIDE) in a project so that it can be "seen" by all the project modules?
Thanks in advance!
You need to use compiler's -lib commandline option for creating a static library.
Then you can use the static library by passing it as -l mylib parameter when writing a program that uses that library. (The .a needs to be in same directory as .bas or you can put the .a in freebasic/lib/_yourOs_/ folder
Here's a very simple example :
This is the library code.
test.bas :
The header for library. (good practice to have all declarations in a .bi)
test.bi :
Compile the test.bas using -lib parameter at command line
fbc test.bas -lib
This will create a libtest.a file as output.
This is the program that uses the above library.
libtester.bas
Compile this file using
fbc libtester.bas -l test
and make sure .a is in same folder or in freebasic/lib/_youros_/ folder
Edit: Read more about it here
http://www.freebasic.net/wiki/wikka.php ... tPgCompOpt
Then you can use the static library by passing it as -l mylib parameter when writing a program that uses that library. (The .a needs to be in same directory as .bas or you can put the .a in freebasic/lib/_yourOs_/ folder
Here's a very simple example :
This is the library code.
test.bas :
Code: Select all
Function returnone() As Integer
Return 1
End Function
test.bi :
Code: Select all
Declare Function returnone() As Integer
fbc test.bas -lib
This will create a libtest.a file as output.
This is the program that uses the above library.
libtester.bas
Code: Select all
#Include "test.bi"
Print returnone()
Sleep
fbc libtester.bas -l test
and make sure .a is in same folder or in freebasic/lib/_youros_/ folder
Edit: Read more about it here
http://www.freebasic.net/wiki/wikka.php ... tPgCompOpt
Alternative for program using the library:
libtester.bas
Compile this file using:
fbc libtester.bas
See documentation:
http://www.freebasic.net/wiki/wikka.php ... eyPgInclib
libtester.bas
Code: Select all
#Include "test.bi"
#Inclib "test"
Print returnone()
Sleep
fbc libtester.bas
See documentation:
http://www.freebasic.net/wiki/wikka.php ... eyPgInclib
-
- Posts: 21
- Joined: Mar 31, 2011 17:30
- Location: Rome, Italy
I compiled a static library: mylib.bas
I have tried:
#INCLIB "mylib"
and the compiler spits out
cannot find -llibmylib.a
So, I copied "libmylib.a" to
c:\freebasic\libmylib.a
c:\freebasic\lib\win32\libmylib.a
c:\program files\freebasic\lib\win32\libmylib.a
and the result:
cannot find -llibmylib.a
I deleted the statement #INCLIB "mylib"
from my program, and on the command line added -a libmylib.a
Command executed:
"C:\FreeBasic\fbc.exe" "F:\freebasic\Test177.bas" -exx -a libmylib.a
The program compiles and runs.
If I cannot add an object file to the linker list without using the command line. As such, #INCLIB or #LIBPATH are of no value.
I have erased/deleted the FreeBasic installation & then re-installed FreeBasic. Same Results.
What must be done to FreeBasic DURING installation so that it will recognize #inclib "mylib" and run
without requiring the additional option on the command line?
I have tried:
#INCLIB "mylib"
and the compiler spits out
cannot find -llibmylib.a
So, I copied "libmylib.a" to
c:\freebasic\libmylib.a
c:\freebasic\lib\win32\libmylib.a
c:\program files\freebasic\lib\win32\libmylib.a
and the result:
cannot find -llibmylib.a
I deleted the statement #INCLIB "mylib"
from my program, and on the command line added -a libmylib.a
Command executed:
"C:\FreeBasic\fbc.exe" "F:\freebasic\Test177.bas" -exx -a libmylib.a
The program compiles and runs.
If I cannot add an object file to the linker list without using the command line. As such, #INCLIB or #LIBPATH are of no value.
I have erased/deleted the FreeBasic installation & then re-installed FreeBasic. Same Results.
What must be done to FreeBasic DURING installation so that it will recognize #inclib "mylib" and run
without requiring the additional option on the command line?
@integer
What IDE are you using (FbEdit, FBIDE, etc)? If none, what directory are you in when you try to compile the file?
The c:\freebasic\lib\win32\libmylib.a should have worked. Having libmylib.a in the same directory as Test177.bas should also work.
I just did a quick check and it worked first time. All the files are in the same directory.
What IDE are you using (FbEdit, FBIDE, etc)? If none, what directory are you in when you try to compile the file?
The c:\freebasic\lib\win32\libmylib.a should have worked. Having libmylib.a in the same directory as Test177.bas should also work.
I just did a quick check and it worked first time. All the files are in the same directory.
Code: Select all
' Filename: mylib.bi
Declare Function add_number(ByVal x As Single, ByVal y As Single) As Single
' -----------------------------------------
' Filename: mylib.bas
' Compiled with: D:\Programs\FreeBASIC\fbc -lib "mylib.bas"
#Include "mylib.bi"
Function add_number(ByVal x As Single, ByVal y As Single) As Single
Return x + y
End Function
' -----------------------------------------
' Filename: test.bas
' Compiled with: D:\Programs\FreeBASIC\fbc "test.bas"
#Inclib "mylib"
#Include "mylib.bi"
Print add_number(1, 2)
Sleep
Almost exactly the same as my previous example. Just add Export to the end of the functions in the library that you wish to make visible. And change the -lib compiler switch to either -dll or -dylib (both do the same thing).
There is lots of information about it here:
http://www.freebasic.net/wiki/wikka.php ... dLibraries
If you want to link your FB made dynamic library (*.dll or *.so) to some other language such a C then you may want to also investigate Alias, Cdecl, Stdcall and Extern "foo" ... End Extern
Code: Select all
' Filename: mylib.bi
Declare Function add_number(ByVal x As Single, ByVal y As Single) As Single
' -----------------------------------------
' Filename: mylib.bas
' Compiled with: D:\Programs\FreeBASIC\fbc -dylib "mylib.bas"
#Include "mylib.bi"
Function add_number(ByVal x As Single, ByVal y As Single) As Single Export
Return x + y
End Function
' -----------------------------------------
' Filename: test.bas
' Compiled with: D:\Programs\FreeBASIC\fbc "test.bas"
#Inclib "mylib"
#Include "mylib.bi"
Print add_number(1, 2)
Sleep
http://www.freebasic.net/wiki/wikka.php ... dLibraries
If you want to link your FB made dynamic library (*.dll or *.so) to some other language such a C then you may want to also investigate Alias, Cdecl, Stdcall and Extern "foo" ... End Extern
Linking libraries must currently be done using the -l option, for example -l mylib. Passing the .a file directly unfortunately is broken at the moment (see also this bug report), it will be fixed for the next release though.