C中如何调用C++函数
2015-01-22 12:28
176 查看
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;
}
将 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;
}
相关文章推荐
- C中如何调用C++函数
- C中如何调用C++函数
- C中如何调用C++函数
- c++函数调用到底是如何编译的!
- 如何在C中调用C++函数 与 如何在C++中调用C的函数(待修改)
- 部分转载:如何实现C和C++函数互相调用
- C中如何调用C++函数、类内函数
- C中如何调用C++函数
- C中如何调用C++函数、类内函数
- 部分转载:如何实现C和C++函数互相调用
- c++函数调用的返回值在函数调用栈上如何传递给调用者(返回大对象时)
- C中如何调用C++函数
- C中如何调用C++函数
- C中如何调用C++函数、类内函数
- C中如何调用C++函数
- 在lldb调试中调用c++函数 - 如何使用QuartzCore里面的日志消息
- C中如何调用C++函数
- C中如何调用C++函数
- 技巧:C中如何调用C++函数
- C程序中如何调用C++函数