C Static library experiment.. so close!

BlitzMax Forums/BlitzMax Programming/C Static library experiment.. so close!

SurreaL(Posted 2005) [#1]
Hello! I've tried to play around with compiling a static lib w/ gcc, and using it in blitzmax. It seems I am failing :P Hopefully some kind soul out there in blitzland will be able to help me out :D (Drago..? anyone..? ..bueller?)

Anyways this is what I've done. I have the following extremely simple source file to work with in c:
//testsource.c
int testfunc(int a,int b)
{
    return a + b;
}
I pieced together the following steps from several tutorials on compiling/linking static libs.. so bare with me.

First, I went and compiled the .c file into a .o object file using gcc. Afterwards I used the ar program to throw the object file into a .a file, which is in my understanding an archive of object files. (Or basically a static library)

Then I went and created the following path in my blitzmax folder.. \mods\surreal.mod\test.mod , and placed the static lib file (statictest.a) within it. Along with this I've gone and created test.bmx, and placed the following code inside:
Strict

Module surreal.test

Import "-lstatictest"

Extern
	Function testfunc:int (a, b)
End Extern
After saving this module, I've been able to successfully compile it using "bmk makemods -r surreal.test", with no errors. So far so good.. (Or so I'd like to believe)

Anyways on to test it. I create a new .bmx file which looks like this:
Import surreal.test

Print testfunc (1,2)
As you can see I'm striving for the simplest code here, just trying to get the pipeline down.. and this is where I run into problems. Upon trying to compile this example, I get the following compile error:


Compile Error:Can't find interface for module 'surreal.test'


I've triple checked the path involved (bmaxroot\mod\surreal.mod\test.mod), and it contains the following files:
libstatictest.a
test.a
test.bmx
test.i

and within surreal.mod\test.mod\.bmx:
test.bmx.i
test.bmx.o
test.bmx.s

I have only tried compiling in release mode, as I realize there is no debug mode version of the lib to compile with. (I've noticed how there are two versions of each module, obviously the '.d.' in the filename denotes debug versions in other mods)

Other things I've tried, with no effect:
-Copying libstatictest.a into .bmx dir. (Didn't think this would work since I've never seen a .a file in a .bmx folder.. but hey)
-Using Extern "Win32" instead of just Extern
-Renaming everything since originally I named the function, module, and library "test"
-Banging my head against the wall.

Sadly, I'm running out of ideas.. so if anyone could help me out, I'd be very greatful.

Thanks in advance :)


SurreaL(Posted 2005) [#2]
p.s. Ok turns out the problem was just me being a moron.. I've updated bmax to the 1.03 version today, but I placed it in a new folder, and hadn't told Protean about it. (I updated my paths vars.. oops only half the battle!)

Anyways. After doing so, I got an error in compilation showing that -lstatictest could not be found. Turns out all I had to do was 'Import "libstatictest.a"', and all was well! So.. everything's working :D

To re-iterate the method in case anyone else is wondering how this was done..

Step 1) Take source code, and compile using something along the lines of "gcc -c mysource.c". This results in an object file.
Step 2) Pack any object files into an archive of them by calling "ar rc mylib.a mysource.o mysource2.o" etc.. you can add as many objects into the archive as you like.
Step 3) Place the resulting .a file into a module directory, and create a module (.bmx file) which Externs the functions from the static library.
Step 4) Use the function in any bmax source you write from there :)

*Remember if you are using the "Framework" command to stop Blitz from loading ALL modules as per default, you will have to explicitly Import your module for it to still work.


Perturbatio(Posted 2005) [#3]
move strict below the Module declaration and it works.


Zenith(Posted 2005) [#4]
Import "-lstatictest"
Would work if you had put it in the Blitzmax/lib/ folder. :D
And it was named "libstatictest.a" I think.