python调用C++
由于C++支持函数重载,用g++编译C++时编译器会给函数的名称附加上额外的信息,这样ctypes模块就会找不到g++编译生成的函数。因此,要让g++按照C语言的方式编译才可以找到生成的函数名。让编译器以C语言的方式编译就要在代码中使用
extern关键字
将代码包裹起来。
编写c++源程序
//test.cpp
#include <iostream>
using namespace std;
class TestLib
public:
void display();
void display(int a);
void TestLib::display() {
cout<<"First display"<<endl;
void TestLib::display(int a) {
cout<<"Second display:"<<a<<endl;
extern "C" {
TestLib obj;
void display() {
obj.display();
void display_int(int a) {
obj.display(a);
编译c++源程序
g++ -o test.so -shared -fPIC test.cpp
编译参数说明:
-fPIC:生成位置无关目标代码,适用于动态连接;
-L path:表示在path目录中搜索库文件,如-L.表示在当前目录;
-I path:表示在path目录中搜索头文件;
-o file:制定输出文件为file;
-shared:生成一个共享库文件;
python调用编译库
import ctypes
dll = ctypes.cdll.LoadLibrary
lib = dll('../C++/test.so')
lib.display()
lib.display_int(0)
使用boost实现
首先boost是一个C++第三方开源库,参考:C++:Boost库。这里使用的是其中的跨语言混合编程功能
编写C++源程序
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
int Add(const int x, const int y)
return x + y;
int Del(const int x, const int y)
return x - y;
BOOST_PYTHON_MODULE(test2)
def("Add", Add);
def("Del", Del);
编译C++源程序
g++ test.cpp -fPIC -shared -o test.so -I/usr/bin/python2.7 -I/usr/local/include -L/usr/local/lib -lboost_python
编译时需要指定boost头文件和库的路径,我这里分别是/usr/local/include和/usr/local/lib
(1)报错1
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
(2)报错2
python引入链接库
>>> import test2
>>> test2.Add(1,2)
>>> test2.Del(1,2)
1、简单的Python调用C++程序
2、c语言将程序写为动态库,C语言函数篇(五)静态库和动态库的创建和使用
3、Python调用C++程序的几种方法