C/C++混合编程(一)
来源:互联网 发布:苹果电脑淘宝不能聊天 编辑:程序博客网 时间:2024/05/16 04:38
一、如何在C++中调用已经被编译好的C函数?下面的小例子展示了这个过程:
1. 假设某C函数如下定义
/* file foo.c*/#include <stdio.h>int add(int x, int y){ return x + y;}
编译并生成DLL:
gcc -fpic –c foo.cgcc –shared -o foo.dll foo.o
2. 在C++中调用这个被编译后的C函数
// file test.cpp#include <iostream>extern "C"{ int add(int x, int y);}int main(){#ifdef __cplusplus std::cout << "C++ defined" << std::endl;#else std::cout << "C++ undefined" << std::endl;#endif std::cout << add(1, 2) << std::endl; return 0;}
编译并生成可执行文件:
g++ -o test.exe test.cpp foo.dll
3. 原因分析:
函数add()被C编译器编译后在库中的名字为_add,而C++编译器则会产生_add_int_int之类的名字用来支持函数重载和类型安全连接。由于编译后的名字不同,C++程序不能直接调用C函数。C++提供了一个C连接交换指定符号extern “C” 来解决这个问题。
二、如何在C中调用已经被编译好的C++函数?下面的小例子展示了这个过程:
C调用C++函数库,一般不能直接调用,需要将C++库转换成C接口输出,方可以使用C调用,看下面的例子:
- aa.cxx
#include "add.h"int sample::method(){ cout<<"method is called!\n";}
- aa.h
#include <iostream>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"#endifint myfunc(){ sample ss; ss.method(); return 0;}
- mylib.h
#ifdef _cplusplusextern "C"{#endifint 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 <stdio.h>#include <dlfcn.h>#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()函数也能执行,这里介绍的是常规调用库函数的方法。
0 0
- C/C++混合编程(一)
- c/c++混合编程
- C/C++混合编程
- C/C++混合编程
- C 汇编混合编程
- C/C++混合编程
- C++&Java混合编程
- C/C++混合编程
- C/C++混合编程
- C/C++混合编程
- C/C++混合编程
- C/C++混合编程
- C/C++混合编程
- C&C++混合编程
- C&C++混合编程
- C&C++混合编程
- Matlab C混合编程
- C、C++混合编程
- IT行业 缓解工作压力
- 一致性hash算法 - consistent hashing
- 触屏setTouchEable(True)
- vim 替换操作大全
- mssql errorlog导致磁盘空间不足 与 主文件组不足 的解决
- C/C++混合编程(一)
- shell 数组
- 求字符串长度并返回
- erlang gen_fsm
- Flex Rect用法
- CCMenuItemImage使用
- 利用PhpExcel读取Excel中图片文件
- HBase性能调优
- MFC控件积累——CComboBoxEx控件