如果你有一個c++做的動態連結庫檔案,而你只有一些相關類的宣告,那麼你如何用c呼叫呢?下面是小編為大家整理的怎麼在C語言中呼叫C++做的'動態連結庫的相關內容,歡迎參考~
連結庫標頭檔案:
head.h
class A
{
public:
A();
virtual ~A();
int gt();
int pt();
private:
int s;
};
#include
#include "head.h"
A::A(){}
A::~A(){}
int A::gt()
{
s=10;
}
int A::pt()
{
std::cout<<s<<std::endl;
}
編譯命令如下:
g++ -shared -o
這時候生成檔案,將其拷貝到系統庫裡面:/usr/lib/
進行二次封裝:
#include
#include "head.h"
extern "C"
{
int f();
int f()
{
A a;
();
();
return 0;
}
}
編譯命令:
gcc -shared -o -L. -lmy
這時候生成第二個檔案,此時庫從一個類變成了一個c的介面.
拷貝到/usr/lib
下面開始呼叫:
test.c
#include "stdio.h"
#include "dlfcn.h"
#define SOFILE ""
int (*f)();
int main()
{
void *dp;
dp=dlopen(SOFILE,RTLD_LAZY);
f=dlsym(dp,"f");
f();
return 0;
}
編譯命令如下:
gcc -rdynamic -s -o myapp test.c
執行Z$./myapp
10
$