Compiling a library

New to FreeBASIC? Post your questions here.
Post Reply
Fabrizio_00000
Posts: 21
Joined: Mar 31, 2011 17:30
Location: Rome, Italy

Compiling a library

Post by Fabrizio_00000 »

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!
nkk_kan
Posts: 209
Joined: May 18, 2007 13:01
Location: India
Contact:

Post by nkk_kan »

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 :

Code: Select all

Function returnone() As Integer
	Return 1
End Function
The header for library. (good practice to have all declarations in a .bi)

test.bi :

Code: Select all

Declare Function returnone() As Integer
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

Code: Select all

#Include "test.bi"

Print returnone()
Sleep
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
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

Alternative for program using the library:

libtester.bas

Code: Select all

#Include "test.bi"
#Inclib "test"

Print returnone()
Sleep
Compile this file using:
fbc libtester.bas


See documentation:
http://www.freebasic.net/wiki/wikka.php ... eyPgInclib
Fabrizio_00000
Posts: 21
Joined: Mar 31, 2011 17:30
Location: Rome, Italy

Post by Fabrizio_00000 »

Thank you very much! Both of you have been clear and exhaustive.
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Post by integer »

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?
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

@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.

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
darkhog
Posts: 132
Joined: Oct 05, 2011 21:19

Post by darkhog »

I'd like to ask related question - how to write dynamic library (.dll in windows, .so in most unixes like Linux)?
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

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).

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
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
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Post by dkl »

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.
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

@dkl
I'm guessing this doesn't affect #Inclib
Post Reply