在Cython中使用C ++ STL映射 [英] Using C++ STL maps in Cython

查看:219
本文介绍了在Cython中使用C ++ STL映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Cython类中使用映射,但是Cython编译器会引发错误。

I'm trying to use a map in a Cython class but the Cython compiler raises an error.

下面是一个示例,演示了我的问题,并且报告了错误Cython。

Here is an example demonstrating my problem and the error reported by Cython.

Cython文件pyx

from libcpp.map cimport map
from libcpp.utility cimport pair
from libcpp.string  cimport string

cdef class MyDict:
      cdef:
            map[string, int] components

      def __setitem__(self, key, value):
            self.components[key] = value

      def __getitem__(self, key):
            return self.components[key]

Python文件

from pyximport import install
install()

from dic_class import MyDict

m = MyDict()

m["home"] = 5

print m["home"]

Cython报告的错误


严重错误:实用工具:没有这样的文件或目录

fatal error: utility: No such file or directory


推荐答案

您无需做任何设置即可将其设置为C ++而不是C。C编译器将无法找到C ++标准库(因此会出现无此类文件或目录错误)。

You've not done anything to set it up to compile as C++ rather than C. The C compiler will be unable to find the C++ standard library (hence the "no such file or directory" error).

您需要 tell pyximport通过设置.pyxbld文件来使用C ++而不是C 。或者,您可以使用 C ++模式下的setup.py 而不是pyximport。

You need to tell pyximport to use C++ instead of C by setting up a .pyxbld file. Alternatively you could use setup.py in C++ mode instead of pyximport.

这篇关于在Cython中使用C ++ STL映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆