好多开源的项目要用,但是没有Lazarus的头文件。在win下只能用VC自己写一个接口动态库。在Linux环境下怎么写这些接口库,下面是一个简单的方法:
一、用VC编译好源程序,并用Lazarus成功调用。
二、新建一个cpp
1、引用等按VC进行编写
2、dll中的extern "C" __declspec(dllexport)删除
3、用extern "C"{}把所有的函数包括。
#include<aa.h>
#include<bb.h>
extern "C"{
int test1(const char* ff){
return 0;
}
int test2(const char* ff){
return 0;
}
}
三、编译
转到编译目标环境运行
gcc -fPIC -shared -o libMy.so my.cpp -I 包含文件目录 -L 使用到的动态库目录 -l 使用到的动态库名 -lstdc++ -std=c++11
使用到的动态库名写法:去掉前面的lib和后面的.so。比如
libgomp.so.1 写成 gomp
生成so文件,Lazarus调用时只要改一下动态库文件名就可以了。
const
{$IfDef WINDOWS}
DllFileName='My.dll';
{$Else}
DllFileName='libMy.so';
{$EndIf}
function Dlltest1(File: PChar): integer; cdecl; external DllFileName Name 'test1';
function Dlltest2(File: PChar): integer; cdecl; external DllFileName Name 'test2';