问题描述: 我想访问的Python C++对象,使用Cython。
当运行python2 setup.py build_ext -i我得到了Cython.Compiler.Errors.CompileError用Cython返回上双``一个Cython.Compiler.Errors.CompileError`(s.time_since_epoch()计数()。)`
你可以有下面一起来看看,并告诉我,我应该怎样改变以获得用Cython编译我的代码?
当我尝试用Cython编译,我得到:
$ python2 setup.py build_ext -i
Compiling ds5.pyx because it changed.
[1/1] Cythonizing ds5.pyx
Error compiling Cython file:
------------------------------------------------------------
...
cdef cppclass os_time_service:
#double get_time() const override
double get_time()
#static_cast(ts.time_since_epoch().count())
double (s.time_since_epoch().count())
^
------------------------------------------------------------
ds5.pyx:15:37: Expected '.', found 'count'
Traceback (most recent call last):
File "setup.py", line 5, in
ext_modules = cythonize("ds5.pyx"))
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 934, in cythonize
cythonize_one(*args)
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 1056, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: ds5.pyx
在h文件中的相关行是:
$ view backend.h
#ifndef LIBREALSENSE_BACKEND_H
#define LIBREALSENSE_BACKEND_H
#include "../include/librealsense/rs.h" // Inherit all type definitions in the public API
#include // For shared_ptr
#include // For function
#include // For this_thread::sleep_for
#include
#include
#include
#include
const uint16_t VID_INTEL_CAMERA = 0x8086;
namespace rsimpl
{
namespace uvc
{
class time_service
{
public:
virtual double get_time() const = 0;
~time_service() = default;
};
class os_time_service: public time_service
{
public:
double get_time() const override
{
auto ts = std::chrono::time_point_cast<:chrono::milliseconds>(std::chrono::system_clock::now());
return static_cast(ts.time_since_epoch().count());
}
};
...
而且我PYX文件是:
$ cat ds5.pyx
# distutils: language = c++
# distutils: sources = uvc-v4l2.cpp
cdef extern from "backend.h" namespace "rsimpl::uvc":
#const uint16_t VID_INTEL_CAMERA = 0x8086
const unsigned short VID_INTEL_CAMERA = 0x8086
cdef cppclass time_service:
#virtual double get_time() const = 0
double get_time()
cdef cppclass os_time_service:
#double get_time() const override
double get_time()
#static_cast(ts.time_since_epoch().count())
double (s.time_since_epoch().count())
cdef class os_time_service_c:
cdef os_time_service *_os_time_service_ptr
def __cinit__(self):
self._os_time_service_ptr = new os_time_service()
if self._os_time_service_ptr == NULL:
raise MemoryError()
def __dealloc__(self):
if self._os_time_service_ptr != NULL:
del self._os_time_service_ptr
cpdef double get_time(self):
return self._os_time_service_ptr.ts.time_since_epoch().count()
环境:
Ubuntu 16.04
Python 2.7.12
Cython 0.25.2