How do I compile a C project as a static lib for inclusion..

New to FreeBASIC? Post your questions here.
uten
Posts: 7
Joined: May 01, 2006 18:45

How do I compile a C project as a static lib for inclusion..

Postby uten » Nov 24, 2006 13:11

How do I compile a C project as a static lib for inclusion in a FreeBASIC project?

I'm not able to figure out how to compile a static library in C so that the library will be accessable in a FreeBASIC project. It is probably a small issue. Some setting I have missed or flag that is not set.

This is done on a windows 2000 installation with sp4. I have not updated the machine with the latest patches (Automatic Update) from Microsoft. I have used the latest Current dirtribution from mingw32 and Freebasic 0.16b and 0.16b patched with 0.17*.

To figure out what goes wrong I have created a small test project http://fileanchor.com/81165-d. When Including my static library in the FreeBASIC project I get these error's

Code: Select all

C:\slettes\statictest>"C:\FreeBasic16b\fbc.exe"     "statictesttest.bas"
statictesttest.o:fake:(.text+0x3d): undefined reference to `SAMPLEADDINT@8'
statictesttest.o:fake:(.text+0x4a): undefined reference to `SAMPLEFUNCTION1@0'
statictesttest.o:fake:(.text+0x67): undefined reference to `SAMPLEFUNCTION2@0'
Press any key to continue . . .



I have created a makefile to compile the projects. Including a sample in C using the static library. Note the config lines at the top which has to be modified to suite your setup.

Code: Select all

@REM TODO: Set PATH's for this session.
SET PATH=C:\mingw32\bin;c:\mingw32\mingw32\bin
SET MINGW_INCLUDE="C:/MinGW32/include"
SET MINGW_LIB="C:/MinGW32/lib"

@REM
@REM fbc testing SET fbc="C:\portableapps\FreeBASIC\fbc.exe"
SET fbc="C:\FreeBasic16b\fbc.exe"
@echo *** Verify pat's to compilers
@pause
@echo off

@REM
@REM Remove old files
DEL /F *.o  *.a UsingStaticTest.exe

@REM
@REM Create static lib from c source
gcc.exe -c static.c -o static.o -I%MINGW_INCLUDE%

@REM
@REM ar: creating libstatictest.a
ar r libstatictest.a static.o

@REM
@REM No nead for ranlib anymore? ar is supposed to take care of it
ranlib libstatictest.a


@REM
@REM Create a test with a C file

gcc.exe -c UsingStaticTest.c -o UsingStaticTest.o -I%MINGW_INCLUDE%
gcc.exe UsingStaticTest.o -o "UsingStaticTest.exe" -L%MINGW_LIB% libstatictest.a

@echo ===============================================
@echo RUnning C sample
@echo ===============================================
UsingStaticTest.exe

@echo ==============================================
@echo Creating FreeBASIC sample
@echo ===============================================
@REM I thought this explicit reference is unnecessary as I use #inclib?
SET fbcop= -I statictest
SET fbcfl="statictesttest.bas"
%fbc% %fbcop% %fbcfl%
@echo ===============================================
@echo RUnning FreeBASIC sample
@echo ===============================================
statictesttest.exe
@pause


static.c: The C file to become a static library.

Code: Select all

/* A function adding two integers and returning the result */
#include "static.h"
int SampleAddInt(int i1, int i2)
{
   return i1 + i2;
}

/* A function doing nothing ;) */
void SampleFunction1()
{
   /* insert code here */

}

/* A function always returning zero */
int SampleFunction2()
{
   /* insert code here */

   return 10;
}

UsingStaticTest.C: The C test project to verify that the static lib is C compatible.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include "static.h"
int main(int argc, char *argv[])
{
  printf("SampleAddInt(5, 5):=%d\n", SampleAddInt(5, 5));
  system("PAUSE");   
  return 0;
}

statictest.bi: To interface the static library and automatically include it (#inclib "statictest" ) i have this file.

Code: Select all

''include file for libstatictest.a
#IfNDef __statictest_bi__
#Define __statictest_bi__
'' Not sure what is correct notation is for inclib. I think #inclib now at version >=0.16b. Suppose the later one is deprecated?
#inclib "statictest"
'' '$inclib: "statictest"

declare function SampleAddInt(byval i1 as integer, byval i2 as integer) as integer
declare sub SampleFunction1()
declare function SampleFunction2() as integer
#endif

And finally the main FreeBASIC file:
statictesttest.bas:

Code: Select all

''Testing functions in statictest.bi
''#include "statictest.bi"
'$include: "statictest.bi"
''
print "SampleAddInt(10, 10):=", SampleAddInt(10, 10)
'' Just a dumy call
SampleFunction1()
''
print "SampleFunction2():=", SampleFunction2()


Could anyone could point out what I'm doing wrong?

All files in a zip http://fileanchor.com/81165-d

Best Regards
Uten

EDIT: Some of the code came out wrong and the last call in the cmd file missed a part of the name.
<michael>
Posts: 24
Joined: Oct 29, 2005 10:14

Postby <michael> » Nov 24, 2006 13:40

You header file is wrong.

Code: Select all

''include file for libstatictest.a
#IfNDef __statictest_bi__
#Define __statictest_bi__
'' Not sure what is correct notation is for inclib. I think #inclib now at version >=0.16b. Suppose the later one is deprecated?
#inclib "statictest"
'' '$inclib: "statictest"

Declare Function SampleAddInt Cdecl Alias "SampleAddInt" (Byval i1 As Integer, Byval i2 As Integer) As Integer
Declare Sub SampleFunction1 cdecl Alias "SampleFunction1" ()
Declare Function SampleFunction2 Cdecl Alias "SampleFunction2" () As Integer
#endif


Try this ;)
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Nov 24, 2006 14:13

Also, to answer your question in the comment, yes, '$inclib, and all those '$ commands are obsolete in the latest versions of FreeBASIC, and will be ignored as normal comments.
uten
Posts: 7
Joined: May 01, 2006 18:45

Postby uten » Nov 24, 2006 14:53

<michael> wrote:You header file is wrong.

Code: Select all

''include file for libstatictest.a
Declare Function SampleAddInt Cdecl Alias "SampleAddInt" (Byval i1 As

Try this ;)

*slapping my head* I knew It had to be simple *argh*
Thanks a lot..:)

cha0s, thanks for your comments to. Now I can remove that from my "figure out" list.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests