C中如何调用C++函数

来源:互联网 发布:数据还原 编辑:程序博客网 时间:2024/05/22 00:55
1,在C中如何调用C++函数将函数用extern "C"声明;
将 C++ 函数声明为``extern "C"''(在你的 C++ 代码里做这个声明),然后调用它(在你的 C 或者 C++ 代码里调用)。例如:
// C++ code:
extern "C" void f(int);
void f(int i)
{
// ...
}

然后,你可以这样使用 f():
/* C code: */
void f(int);
void cc(int i)
{
f(i);
/* ... */
}



2, 那么如何将类内成员函数声明?
当然,这招只适用于非成员函数。如果你想要在 C 里调用成员函数(包括虚函数),则需要提供一个简单的包装(wrapper)。例如:
// C++ code:
class C
{
// ...
virtual double f(int);
}; 

extern "C" double call_C_f(C* p, int i) // wrapper function
{
return p->f(i);
}

然后,你就可以这样调用 C::f():
/* C code: */
double call_C_f(struct C* p, int i); 
void ccc(struct C* p, int i)
{
double d = call_C_f(p,i);
/* ... */
}

例子:
TVqecPayloadFetcher * vqecPayloadFetcher = NULL;
IVqecStatistics::TVqecStatisticsInfo vqecStatisticsInfo ;

bool Ttest::Myfun(const char* addr, uint32_t port) throw ()
{
vqec_ifclient_get_dcr_info_ops_t dcr_info_ops;
::vqecPayloadFetcher = this;
memset(&vqecStatisticsInfo, 0, sizeof(vqecStatisticsInfo));
dcr_info_ops.get_dcr_stats = GetDcrStats;
::vqec_ifclient_bind_params_set_dcr_info_ops(bp, &dcr_info_ops);



TVqecStatisticsCaller * TVqecPayloadFetcher::GetVqecStatisticsCaller() throw()
{
return VqecStatisticsCaller;
}
/*
* Callback  function supplied during a tuner bind operation.
*/
extern "C" int32_t GetDcrStats (int32_t context_id,
vqec_ifclient_dcr_stats_t *stats)
{
if(vqecPayloadFetcher !=NULL) {

if (vqecPayloadFetcher->GetVqecStatisticsCaller() && vqecStatisticsInfo.DecodedPictureCount == 0) {

vqecPayloadFetcher->GetVqecStatisticsCaller()->GetVqecReport(vqecStatisticsInfo, result);
if (result == ITypes::RESULT_OK) {
if (vqecStatisticsInfo.FirstFrameDisplayTime != 0) {
// Here add your wanted code
stats->dec_picture_cnt  = vqecStatisticsInfo.DecodedPictureCount;
stats->fst_decode_time = vqecStatisticsInfo.FirstFrameDecodeTime;
stats->last_decoded_pts = vqecStatisticsInfo.LastDecodedPTS;
stats->display_time  = vqecStatisticsInfo.FirstFrameDisplayTime;
stats->display_pts  = vqecStatisticsInfo.FirstDisplayedPTS;
fprintf(stderr, "%s:%d:%s,  vqec stats info   if(temp !=NULL)  get the vqec statistics info here. \n",__FILE__, __LINE__,__FUNCTION__ );
} else {
fprintf(stderr, "%s:%d:%s,  vqec stats info   if(temp !=NULL) not get  the vqec statistics info here.\n",__FILE__, __LINE__,__FUNCTION__ );
}


}

fprintf(stderr, "%s:%d:%s,  vqec stats info  else if(temp !=NULL)  \n",__FILE__, __LINE__,__FUNCTION__ );
//stats->dec_picture_cnt = 15;
}  else{
fprintf(stderr, "%s:%d:%s,  vqec stats info  else if(temp !=NULL)  \n",__FILE__, __LINE__,__FUNCTION__ );
stats->dec_picture_cnt = 18;
}



3, 如果你想在 C 里调用重载函数,则必须提供不同名字的包装,这样才能被 C 代码调用。例如:
// C++ code:
void f(int);
void f(double);

extern "C" void f_i(int i) { f(i); }

extern "C" void f_d(double d) { f(d); }

然后,你可以这样使用每个重载的 f():
/* C code: */
void f_i(int);
void f_d(double); 

void cccc(int i,double d)
{
f_i(i);
f_d(d);
/* ... */
}
注意,这些技巧也适用于在 C 里调用 C++ 类库,即使你不能(或者不想)修改 C++ 头文件。




4, 如何在c程序中调用c++的库
假如我们手上有一个c++的类
//test1.h
class Test
{
public:
void test();
};

//test1.cc
#include "test1.h"
void Test::test();
{
printf("just a test!\n");
}

我们需要用一个C程序来调用这个c++库里面的函数。
A. 将C++库做C的封装。
//test2.c
#include "test1.h"
extern "c"
{
void test()
{
Test t;
t.test();
}
}

编译:g++ -shared -o test.so test2.c test1.cc

B. 编写用C代码调用C++的实现
//test.c
#include <dlfcn.h>
#include <stdio.h>
int main(int argc , char ** argv)
{
void * lib = dlopen("./test.so" , RTLD_LAZY);
if (lib == 0)
{
printf("lib is null\n");
return -1;
}

void (* function)();
function = dlsym(lib , "test");
if (function == 0)
{
printf("function == 0\n");
return -1;
}

function();

dlclose(lib);

return 0;
}

0 0
原创粉丝点击