Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

Using C++ library in C code

14 Janvier 2013 Publié dans #c-c++

extern “C” can help you to call c fonction from c++, and also it can help you do the reverse. But as we know c++ has class which we cannot use in c, so we have to do some wrapper to help us.

1. //code in add.cxx

2. #include "add.h"

3. int sample::method()

4. {

5. cout<<"method is called!\n";

6. }

1. //code in add.h

2. #include <iostream>

3. using namespace std;

4. class sample

5. {

6. public:

7. int method();

8. };

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

sudo g++ -fpic -shared -g -o /usr/lib/libadd.so add.cxx -I ./

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

1. //code in mylib.cxx

2. #include "add.h"

3. #ifndef _cplusplus

4. #define _cplusplus

5. #include "mylib.h"

6. #endif

7.

8. int myfunc()

9. {

10. sample ss;

11. ss.method();

12. return 0;

13. }

1. //code in mylib.h

2. #ifdef _cplusplus

3. extern "C"

4. {

5. #endif

6.

7. int myfunc();

8.

9. #ifdef _cplusplus

10. }

11. #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 ./

1. main.c

2. #include <stdio.h>

3. #include <dlfcn.h>

4. #include "mylib.h"

5.

6. int

7. main()

8. {

9. int (*dlfunc)();

10. void *handle; //定义一个句柄

11. handle = dlopen("./mylib.so", RTLD_LAZY);//获得库句柄

12. dlfunc = dlsym(handle, "myfunc"); //获得函数入口

13. (*dlfunc)();

14. dlclose(handle);

15.

16. return 0;

17. }

编译命令如下:

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()函数也能执行,这里介绍的是常规调用库函数的方法。

Publicité
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article
T
test
Répondre