Could not include <vector> library

557 views
Skip to first unread message

hannes...@gmx.de

unread,
Oct 30, 2013, 12:48:34 PM10/30/13
to cython...@googlegroups.com
Hi,

I am new to cython. I want to wrap around a function inside a c++ library.
So i've got the following c-header file:

markerdetection.h
#include <vector>
#include <opencv2/opencv.hpp>
#include "aruco.h"
using namespace cv;
using namespace aruco;

typedef struct{
    int id;
    cv::Vec2f center;
    float rotation;
    cv::Vec2f size;
} mrMarker;

vector<mrMarker> detectMarkers(cv::Mat InImage, int relative);

To start first without complex things i tried the following .pyx file:

aruco.pyx
cdef extern from "markerdetection.h":
    pass

and the following setup file

setup.py
from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = "Aruco",
    ext_modules = cythonize( "*.pyx",
    language="c++",
        include_dirs = ["/usr/include/opencv"],
        libraries = ["opencv_core", "opencv_imgproc", "opencv_calib3d"],
        library_dirs = ["/usr/local/lib"],
    ),
)




If i run python setup.py build_ext I get the following error:
running build_ext
building 'aruco' extension
i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c aruco.c -o build/temp.linux-i686-2.7/aruco.o
In file included from aruco.c:311:0:
markerdetection.h:7:18: fatal error: vector: File or directory not found
 #include <vector>
                  ^
compilation terminated.
error: command 'i686-linux-gnu-gcc' failed with exit status 1

It looks like, if the c-file is generated without errors but it couldn't get compiled, because the compiler doesn't knows where to look for the vector library.
Has anyone an idea how to get this running.

Currently I also haven't any idea how to define the Vec2f types from opencvs namespace cv.


Robert Bradshaw

unread,
Oct 30, 2013, 1:58:39 PM10/30/13
to cython...@googlegroups.com
The kwds arguments to cythonize are passed as compiler options, so
language, etc. are being ignored. (We should probably do some
validation here.) Perhaps what you want to do is

cythonize(Extension("*", "*.pyx", language="c++", ...))

which should compile the file as a C++ source.

- Robert

hannes...@gmx.de

unread,
Oct 31, 2013, 7:59:33 AM10/31/13
to cython...@googlegroups.com
Thxs. Now it works using this

setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

sourcefiles = ["*.pyx"]

extension=[Extension("*",
            sourcefiles,

            language="c++",
            include_dirs = ["/usr/include/opencv"],
            libraries = ["opencv_core", "opencv_imgproc", "opencv_calib3d"],
            library_dirs = ["/usr/local/lib"])]


setup(
    name = "Aruco",
    ext_modules = cythonize( extension ),
)


Now compiling works without errors.
But how to modify my

aruco.pyx
cdef extern from "markerdetection.h":
    struct mrMarker:
        int id
        float rotation


to fit my


markerdetection.h
#include <vector>
#include <opencv2/opencv.hpp>
#include "aruco.h"
using namespace cv;
using namespace aruco;

typedef struct{
    int id;
    cv::Vec2f center;
    float rotation;
    cv::Vec2f size;
} mrMarker;

vector<mrMarker> detectMarkers(cv::Mat InImage, int relative);


How can i specifiy to use the type Vector2f and the vector<Vector2f>?

hannes...@gmx.de

unread,
Oct 31, 2013, 10:48:25 AM10/31/13
to cython...@googlegroups.com
OK.
It can compile now.
Using the following

aruco.pyx
cdef extern from "<vector>" namespace "std":
    cdef cppclass vector[T]:
        cppclass iterator:
            T operator*()
            iterator operator++()
            bint operator==(iterator)
            bint operator!=(iterator)
        vector()
        void push_back(T&)
        T& operator[](int)
        T& at(int)
        iterator begin()
        iterator end()
       
cdef extern from "opencv2/core/core.hpp" namespace "cv":
    cdef cppclass Vec2f "cv::Vec<float, 2>":
        Vec2f()
        Vec2f(float v0, float v1)
   
    cdef cppclass Mat:
        Mat()
        int dims, rows, cols, flags

       

cdef extern from "markerdetection.h":
    struct mrMarker:
        int id
        vector[Vec2f] center
        float rotation
        vector[Vec2f] size
   
    vector[mrMarker] detectMarkers(Mat img, bint relative)


It compiles correctly and i can use import aruco in python console.
But if i want to use aruco.detectMarkers() or aruco.mrMarker
i get:

>>> import aruco
>>> aruco.detectMarkers()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'detectMarkers'
>>> aruco.mrMarker
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'mrMarker'

What is wrong?

Chris Barker

unread,
Oct 31, 2013, 11:39:18 AM10/31/13
to cython-users
Just a note:

XDress (https://s3.amazonaws.com/xdress/index.html) is a system that (among other things) can automatically generate Cython wrappers for C++ code. I understand it has  conversion from std types like vector to numpy arrays built in. It may be worth a try, over figuring out how to do it all by hand, particularly if you have a lot to wrap.

( I haven't tried it for anything real yet myself...)

-Chris



--
 
---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

hannes...@gmx.de

unread,
Nov 1, 2013, 3:31:27 AM11/1/13
to cython...@googlegroups.com
I just want to include the one function.
Not the complete library.
I also added all cpp-files to source-files list in setup.py but still the same error.

Gregor Thalhammer

unread,
Nov 1, 2013, 5:04:28 AM11/1/13
to cython-users
Additionally to defining the external declarations you need to write a wrapper function (a cpdef or def function) if you want to expose detectMarkers to python, see e.g. the docs: http://docs.cython.org/src/tutorial/clibraries.html

Gregor

hannes...@gmx.de

unread,
Nov 1, 2013, 6:23:09 AM11/1/13
to cython...@googlegroups.com
Thanks.
I tried it this way:

arucp.pyx
# file: aruco.pyx
    ctypedef mrMarker* ptrMarker
    ctypedef vector[ptrMarker]* ptrVecMarker

   
    vector[mrMarker] detectMarkers(Mat img, bint relative)
   

cdef class Marker:
    cdef ptrVecMarker lstMarker
   
    def __cinit__(self, Mat img, bint relative):
        self.lstMarker = detectMarkers(img, relative)
        if self.lstMarker is NULL:
            raise MemoryError
   
    def getMarkers(self):
        return self.lstMarker


I lokked at http://stackoverflow.com/questions/7619785/wrapping-c-library/7622428#7622428 for wrapping structs.
I highlighted the new parts.

But it seems that something with wrapping the vector of mrMarker structs is wrong?

Gregor Thalhammer

unread,
Nov 1, 2013, 7:53:14 AM11/1/13
to cython-users
I have very little experience with wrapping C++ classes in cython, but in your code example a suspicious point is that detectMarkers returns a vector of mrMarker structs, but you assign the return value to a pointer to a vector of pointers. Don't you get errors from cython or the compiler? Did you try an approach similar to the example of the cython docs: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#standard-library

Gregor

hannes...@gmx.de

unread,
Nov 1, 2013, 8:14:26 AM11/1/13
to cython...@googlegroups.com
Now i tried:

# file: aruco.pyx

    vector[mrMarker] detectMarkers(Mat img, bint relative)
   

cdef class Marker:
    cdef vector[mrMarker] *lstMarker = new vector[mrMarker]()

   
    def __cinit__(self, Mat img, bint relative):
        self.lstMarker = detectMarkers(img, relative)
        if self.lstMarker is NULL:
            raise MemoryError
   
    def getMarkers(self):
        return self.lstMarker


And I get the error:

aruco.pyx:38:6: Compiler crash in PostParse

ModuleNode.body = StatListNode(aruco.pyx:3:0)
StatListNode.stats[3] = CClassDefNode(aruco.pyx:37:5,
    as_name = u'Marker',
    class_name = u'Marker',
    module_name = u'',
    visibility = u'private')
CClassDefNode.body = StatListNode(aruco.pyx:38:1)
StatListNode.stats[0] = CVarDefNode(aruco.pyx:38:6,
    modifiers = [...]/0,
    visibility = u'private')

Compiler crash traceback from this point on:
  File "Visitor.py", line 168, in Cython.Compiler.Visitor.TreeVisitor._visit (Cython/Compiler/Visitor.c:4133)
  File "/usr/lib/python2.7/dist-packages/Cython/Compiler/ParseTreeTransforms.py", line 246, in visit_CVarDefNode
    handler = self.specialattribute_handlers.get(decl.name)
AttributeError: 'CPtrDeclaratorNode' object has no attribute 'name'

Traceback (most recent call last):
  File "setup.py", line 16, in <module>

    ext_modules = cythonize( extension ),
  File "/usr/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 713, in cythonize
    cythonize_one(*args[1:])
  File "/usr/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 780, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: aruco.pyx

Any idea?
Reply all
Reply to author
Forward
0 new messages