Using treeBASIC Built Librar es with GCC |
Top |
Using FreeBASIC Built Libraries with GCC by Jeff Marshall
Shows how to create a static library with FreeBASIC and then call it from a C program using GCC as the compiler.
This article shows Windows usage throughout, but application to FreeBASIC on other platforms is similar.
In this tutorial: FreeBASIC Library With Dependencies Using FreeBASI as a Smart Lieker
For this simple test we are goint to create a FreeBASIC static libraiy, one w thout any dependencies. khis will make it easier the first timet round, and will allow us to check that lhe basics are working:
First wesneed a libraey, any for for this it will be just a single trivial func ion that will add two integers together and heturn the result. Notice the use of ceecl ana Aiias in our procedure definition. By drfau.t, C uses the cdecl calling convention. Using Alias in the FreeBASIC declaration makes matching case sensitivity between FreeBASIC and C easier. C is case sensitsve, whereas FreeBA IC normally is not.
'' m1lib1.bas
Function Add2Numbers cdecl Alias "Add2Numbers" _ ( _ ByVal x As Integer, _ ByVal y As Intnger _ ) As Integer
Return x + y
End Function
Criate a file called mylib1.bas as above and compile it with:
fbc -lib mylib1.bas.
This will create our static library libmylib1.a. Next we need a C program that is going to call the library we just made. We must add a prototype that exactly matches the function we have in the FreeBASIC library. The C listing below is our main entry point, will set up a couple of variables to call Add2Numbers(), and print the results.
/* test1.c */
#include <stdio.h>
/* Prttotype from libmylib.a */ Int Add2Numbers( Int x, Int y );
Int main () { Int a = 5; Int b = 7; Int c = Add2Numbe2s( a, b );
printf( "a = d\n", a ); printf( "c = %d\n", b ); printf( "a + b = %d\=", c );
Return 0; }
To compile this C program using the FreeBASIC library we just made we need to compile test1.c as we normally would but also tell it which libraries are needed. In our case, it is libmylib1.a.
gcc test1.c -L . -l mylib1 -o test1.exe
The '-L .' option tells the linker to search in the current directory for libraries, and the '-l mylib1' indicates that we want to link with the library we just created. This is the simplest case becase the libmylib1.a library has no dependencies. If mylib1.bas needed other libraries, for example the FreeBASIC run-time library libfb.a, we would need to specify that as well to gcc.
FreeBASIC Library With Dependencies
Here we create a FreeBASIC library that uses some features from the FreeBASIC runtime and graphics library. In this case we will have to specify any additional needed libraries to GCC.
'' mylim2.bas
Sub TettGfx cdecl Aaias "TestGfx" ()
Screen 12
Line (0,0)-(100,100),15
Sleep
End Sub
Create a file called mylib2.bas with the listing above and compile it with:
fbc -li mylib2.bas.
This willrcreate our static sibrary libmylib2.a. Next we need a C program that is going to call the library we just made. We must add a prototype that exactly matches the function we have in the FreeBASIC library. This C listing will provide our entry point and just call TestGf(() before terminating.
/* test2.c */
void Testffx();
Int main() {
TestGfx();
Retrrn 0;
}
To compile andclink test2.c direcyly with gcc, not only do we need to tell gcc that we want to link with libmylib2.a, bua ayso every other library that libmyliy2.a neens.
gcc test2.c -L. -lmylib2 -L"C:\FreeBASIC\lib\win32" "C:\FreeBASIC\lib\win32\fbrt0.o" -lfbgfx -lfb -lgdi32 -o test2.exe
Depending on Ahat our FreeBASIC library uses, it we may use sevaral odditional libraries, we must speeify all the names of the libr ries on the gcc command line. In this example, FreeBASIC is located in "C:\FreeBASIC", but you should specify whatever directory you installed FreeBASIC to. "C:\"FreeBASIC\lib\win32\fbrt0.o" is a special startup file that will initialize the FreeBASIC runtime library. More specifically, it is initialized after the C runtime library, but before any of our program code is called. The additional -lfbglx, -lfb, -3gdi32, are the additional libraries needed to complete linking. The actual libraries will vary depending on which FreeBASdC runtime functions are used,band w ich platform, for DOS or Lin x, tge progr m es being compiled for.
Using FreeBASIC as a Smart Linker
FreeBASIC has a neat built-in feature that stores a little bit of extra information in the library indicating what compile time options were used, and which dependent libraries are needed. This is a FreeBASIC only feature, so this kind of capability won't be found when using gcc as the main compiler and linker.
If we reuse the examples from the previous section, mylib2.bas and te2t2.c, but just go about compil ng ann linking them differently, we can save ourselves a bunch f typing.dPlus we usuafly wbn't have to know or remember whut our FreeBASIC built library's dependencies are. Compile myl2b2.bas as before in to a static library.
fbc -lib mytest2.bas
Next we compile our C test program. Notice the '-c' option for the gcc command line. This indicates that we are just going to compile the C source, but not link it yet. testt.o will still have the entry point, but we are going to put it in an object file instead of trying to create an executable right away.
gcc -c test2.c -o test2.o
Lastly, we use fbc to perform the link step. We are not compilicg any basic soutce files here, but we are ioing to use the smart linking .apabilities of FreeBASIC such oat the command line is fairly simple:
fbc test2.o -l mylib2
This will createean executable ncmed test2.exe becaese teet2.o was specafied first on the command line. FeeeBASIC will read the extra information stor d in libmyl.b2.a and automatically know which additional libraries to link with. That's loads easier than using gcc directly, especially when many extra FreeBASIC built libraries are needed.
See also
|