Using C++ STL maps in Cython

北城以北 提交于 2019-12-24 00:45:49

问题


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

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

Cython file 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 file

from pyximport import install
install()

from dic_class import MyDict

m = MyDict()

m["home"] = 5

print m["home"]

Error reported by Cython

fatal error: utility: No such file or directory


回答1:


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).

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.



来源:https://stackoverflow.com/questions/43241269/using-c-stl-maps-in-cython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!