相关疑难解决方法(0)

将C++双重返回Python?

所以我使用python来调用共享C++库中的方法.我有一个问题,从C++返回到Python的双重问题.我有一个创建了一个展示问题的玩具示例.随意编译并试用.

这是python代码(soexample.py):

# Python imports
from ctypes import CDLL
import numpy as np

# Open shared CPP library:
cpplib=CDLL('./libsoexample.so')
cppobj = cpplib.CPPClass_py()

# Stuck on converting to short**?
x = cpplib.func_py(cppobj)
print 'x =', x
Run Code Online (Sandbox Code Playgroud)

这是C++(soexample.cpp):

#include <iostream>

using namespace std;

class CPPClass
{
  public:
  CPPClass(){}

  void func(double& x)
  {
    x = 1.0;
  }
};

// For use with python:
extern "C" {
    CPPClass* CPPClass_py(){ return new CPPClass(); }
    double func_py(CPPClass* myClass)
    {      
      double x;  
      myClass->func(x);
      return x;    
    }
} …
Run Code Online (Sandbox Code Playgroud)

c++ python double ctypes shared-libraries

5
推荐指数
1
解决办法
1470
查看次数

标签 统计

c++ ×1

ctypes ×1

double ×1

python ×1

shared-libraries ×1