Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66280
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-16 16:29
文章分类
文章存档

2017年(3)

2016年(2)

2015年(17)

我的朋友

分类: WINDOWS

2015-05-08 09:27:17

原文地址:C中如何调用C++函数 作者:mutes

Linux C调用C++库
调用C++函数库,一般不能直接调用,需要将C++库转换成C接口输出,方可以使用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);

   /* ... */

    }

 

当然,这招只适用于非成员函数。如果你想要在 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);

       /* ... */

}

 

如果你想在 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++ 头文件。

 

再看下面的例子:
  • aa.cxx

#include "aa.h"
int sample::method()
{
    cout<<"method is called!\n";
}

  • aa.h

#include

using namespace std;
class sample
{
    public:
    int method();
};

将上面的两个文件生成动态库libaa.so放到 /usr/lib目录下,编译命令如下

    sudo g++ -fpic -shared -g -o /usr/lib/libaa.so aa.cxx -I ./

由于在C中不能识别类,所以要将上面类的成员函数封装成C接口函数输出,下面进行封装,将输出接口转换成C接口。

  • mylib.cxx

#include "add.h"
#ifndef _cplusplus
#define _cplusplus
#include "mylib.h"
#endif

int myfunc()
{
    sample ss;
    ss.method();
    return 0;
}

  • mylib.h

#ifdef _cplusplus
extern "C"
{
#endif

int myfunc();

#ifdef _cplusplus
}
#endif
在linux下,gcc编译器并没用变量_cplusplus来区分是C代码还是C++代码,如果使用gcc编译器,这里我们可以自己定义一个变量_cplusplus用于区分C和C++代码,所以在mylib.cxx中定义了一个变量_cplusplus用于识别是否需要“extern "C"”将函数接口封装成C接口。但是如果使用g++编译器则不需要专门定义_cplusplus,编译命令如下:

      g++ -fpic -shared -g -o mylib.so mylib.cxx -la -I ./

  • main.c

#include
#include
#include "mylib.h"

int
main()
{
    int (*dlfunc)();
    void *handle;       //定义一个句柄
    handle = dlopen("./mylib.so", RTLD_LAZY);//获得库句柄
    dlfunc = dlsym(handle, "myfunc"); //获得函数入口
    (*dlfunc)();
    dlclose(handle);

    return 0;
}
编译命令如下:

gcc -o main main.c ./mylib.so -ldl

下面就可以执行了。

需要说明的是,由于main.c 和 mylib.cxx都需要包含mylib.h,并且要将函数myfunc封装成C接口函数输出需要“extern "C"”,而C又不识别“extern "C"”,所以需要定义_cplusplus来区别处理mylib.h中的函数myfunc。

在main.c的main函数中直接调用myfunc()函数也能执行,这里介绍的是常规调用库函数的方法。


阅读(1652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~