注意!必须进行拼写检查,验证文本,代码和文件名。
这是一个关于如何在C中编写一个简单的库,然后在FreeBASIC中使用它的小型基础教程。应该可以遵循本教程,而无需掌握C或FreeBASIC知识。完成本教程后,您应该可以编译一个静态和动态链接的C库。将必要的头文件转换为FreeBASIC头文件,并了解如何在FreeBASIC项目中使用库。
本教程是用FreeBASIC 0.16b和当时最新的MinGW32版本编写和测试的。作为注释,Dev-cpp使用MinGW32作为编译器工具链。您还可以使用mingw32软件包获取代码::块。
为了演示在FreeBASIC中使用C库,我们需要使用几个函数创建最简单的库。C中的测试文件,以证明我们的图书馆按照预期工作。然后,我们必须将库头文件转换为FreeBASIC头文件(* .bi),最后使用库在FreeBASIC中创建一个测试项目。
所以我们的文件列表将如下所示:
myClib.c:C文件实现我们的库。
myClib.h:C头文件描述库接口。
myClibCTest.c:C文件在C中实现我们的测试程序
myClib.bi:FreeBASIC头文件。myClib.h的翻译。
myClibFBTest.bas:FreeBASIC
make.cmd:编译库和测试文件的示例shell脚本。
C文件成为静态库。myClib.c
(C)
/* A function adding two integers and returning the result */
#include "myClib.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;
}
头文件myClib.h
(C)
int SampleAddInt(int i1, int i2);
void SampleFunction1();
int SampleFunction2();
一个C测试项目来验证静态库是否兼容C。myClibCTest.c:
(C)
#include
#include
#include "myClib.h"
int main(int argc, char *argv[])
{
printf("SampleAddInt(5, 5):=%d\n", SampleAddInt(5, 5));
system("PAUSE");
return 0;
}
将C头文件转换为FreeBASIC头文件myClib.bi:要连接静态库并自动包含它(#inclib“myClib”)我有这个文件。
''include file for libmyClib.a
#ifndef __myClib_bi__
#define __myClib_bi__
#inclib "myClib"
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
最后使用FreeBASIC文件库
myClibFBTest.bas:
''Testing functions in myClib.bi
#include "myClib.bi"
''
Print "SampleAddInt(10, 10):=", SampleAddInt(10, 10)
'' Just a dumy call
SampleFunction1()
''
Print "SampleFunction2():=", SampleFunction2()
make文件:make.cmd我创建了一个批处理文件来编译所有的文件。在C中使用静态库包含一个样本。注意顶部的配置行必须被修改以适应您的设置。
(cmd)
@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 myClibFBTest.exe
@REM
@REM Create static lib from c source
gcc.exe -c myClib.c -o myClib.o -I%MINGW_INCLUDE%
@REM
@REM ar: creating libstatictest.a
ar r libmyClib.a myClib.o
@REM
@REM No nead for ranlib anymore? ar is supposed to take care of it
ranlib libmyClib.a
@REM
@REM Create a test with a C file
gcc.exe -c myClibCTest.c -o myClibCTest.o -I%MINGW_INCLUDE%
gcc.exe myClibCTest.o -o "myClibCTest.exe" -L%MINGW_LIB% libmyClib.a
echo =====================================
echo RUnning C sample
echo =====================================
myClibCTest.exe
echo =====================================
echo Creating FreeBASIC sample
echo =====================================
REM I thought this explicit reference is unnecessary as I use #inclib
SET fbcop= -I myClib
SET fbcfl="myClibFBTest.bas"
%fbc% %fbcop% %fbcfl%
echo =====================================
echo RUnning FreeBASIC sample
echo =====================================
myClibFBTest.exe
@pause
未定义参考尝试链接到静态C库而不使用FreeBASIC头文件中的cdecl别名“functionname”会导致这样的错误。
(cmd)
C:\code>"C:\FreeBasic16b\fbc.exe" "myClibFBTest.bas"
myClibFBTest.o:fake:(.text+0x3d): undefined reference to `SAMPLEADDINT@8'
myClibFBTest.o:fake:(.text+0x4a): undefined reference to `SAMPLEFUNCTION1@0'
myClibFBTest.o:fake:(.text+0x67): undefined reference to `SAMPLEFUNCTION2@0'
Press any key to continue . . .
要解决这个问题,你必须在* .bi文件中找到这样的函数声明:
Declare Function SampleAddInt(ByVal i1 As Integer, ByVal i2 As Integer) As Integer
并将其更改为如下所示:
Declare Function SampleAddInt cdecl Alias "SampleAddInt" (ByVal i1 As Integer, ByVal i2 As Integer) As Integer
本教程的基础是论坛中的几个线程。
当它发展并且可以独立时,线程的链接可能会被删除。
一些有趣的链接,包含在FreeBASIC中创建并由其他语言使用的接口库的信息,反之亦然。
如何将C项目编译为静态库以便包含。