How do I include a C++ class in a namespace into Cython?

Aaron de Windt

I'm trying to create a wrap for a set C++ classes so I can use it in Python using Cython. This is what I have tried so far.

cdef extern from "HilClass.h" namespace "acro":
    cdef cppclass _HilClass "HilClass":
        void start()

cdef class HilClass:
    cdef _HilClass *ptr

    cpdef start(self):
        self.ptr.start()

That doesn't compile. I get the errors below, which by now I've come to learn that a possible cause is that it doesn't find the class. So after checking I had properly included the headers and sources I looked into the .cpp generated by Cython. I searched for acro but was unable to find it. It wasn't mentioned anywhere in the code.

error C2143: syntax error: missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
... (more warning and errors related to HilClass)

This is when I decided to change the second line to.

    cdef cppclass _HilClass "acro::HilClass":

This does compile. But this wasn't done in any example/tutorial I've found. So my question is, I'm I missing something and will my 'workaround' keep working reliably.

NOTE: I'm not interested in creating an instance of this class from within Python or Cython, I just need it to wrap around a HilClass pointer.

EDIT: This is the code inside 'HilClass.h'. 'acrophobe.h' has a few class forward declarations, enums, typedef unint#_t ####, and includes stdint.

#include "acrophobe.h"

namespace acro {
    class HilClass
    {
    public:
        HilClass(AcrophobeClass *acro_ptr);
        ~HilClass();

        HILSocketClass *sck;

        void start();
        void acrophile_iteration(char dest, lbp::PortID_t PortID);

        void set_activity_iteration_start(bool s);
        // More of these set_activity functions
        void set_activity_logging(bool s);


    private:
        AcrophobeClass *acro;

    };
}
J.J. Hakala

You need to define as you have done

cdef cppclass _HilClass "acro::HilClass"

because you are giving acro::HilClass an alias _HilClass. You could test what happens if you define

cdef cppclass HilClass:
    void start()

and then later (the class name is just for an example)

cdef class PyHilClass:
    cdef HilClass * ptr

I don't see any reason why your solution would not be reliable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add a class to a custom namespace?

From Dev

Cython: How do I wrap a C++ class where public member variables are custom objects?

From Dev

How do I include this variable inside this class?

From Dev

Visual Studio c++ how do I find the containing namespace for a given class/method

From Dev

How do I create a custom namespace or class in javascript

From Dev

How do I create a custom namespace or class in javascript

From Dev

How do I disable Cython?

From Dev

How would I include a namespace in an included file?

From Dev

How can I find the namespace for a C# class?

From Dev

How do I get my API to include a child class?

From Dev

How do I include a gem's class method in a Rails model?

From Dev

How do I get my API to include a child class?

From Dev

How do I include 'System.Runtime.Serialization.Json' namespace in my VSCode project on Mac OS X?

From Dev

How do I avoid redundant enums defined in a C# Namespace?

From Dev

Can I do "using namespace.class"?

From Dev

C++ How to have a class relying on a namespace and that namespace relying on the class?

From Dev

How do I compile Pyparsing with Cython on WIndows?

From Dev

How do I compile Pyparsing with Cython on WIndows?

From Dev

How do I include a timer in a universal windows app with c#

From Dev

How to include a namespace in JsonResult

From Dev

How do I make non-templated typedefs in the 'namespace' of a templated class?

From Dev

How do I implement vector <vector <Obj>> for Cython (C++ to Python)?

From Java

How do I declare a namespace in JavaScript?

From Dev

How do I Deserialize Just a String with Namespace?

From Dev

How do i open a repl in a different namespace

From Dev

How do I get an attribute with namespace?

From Dev

How do I control the namespace of a closure in PHP?

From Dev

cython cdef class c method: how to call it from another cython cdef class without python overhead?

From Dev

Do I have to include non standard libraries to class definitions in C++?

Related Related

  1. 1

    How do I add a class to a custom namespace?

  2. 2

    Cython: How do I wrap a C++ class where public member variables are custom objects?

  3. 3

    How do I include this variable inside this class?

  4. 4

    Visual Studio c++ how do I find the containing namespace for a given class/method

  5. 5

    How do I create a custom namespace or class in javascript

  6. 6

    How do I create a custom namespace or class in javascript

  7. 7

    How do I disable Cython?

  8. 8

    How would I include a namespace in an included file?

  9. 9

    How can I find the namespace for a C# class?

  10. 10

    How do I get my API to include a child class?

  11. 11

    How do I include a gem's class method in a Rails model?

  12. 12

    How do I get my API to include a child class?

  13. 13

    How do I include 'System.Runtime.Serialization.Json' namespace in my VSCode project on Mac OS X?

  14. 14

    How do I avoid redundant enums defined in a C# Namespace?

  15. 15

    Can I do "using namespace.class"?

  16. 16

    C++ How to have a class relying on a namespace and that namespace relying on the class?

  17. 17

    How do I compile Pyparsing with Cython on WIndows?

  18. 18

    How do I compile Pyparsing with Cython on WIndows?

  19. 19

    How do I include a timer in a universal windows app with c#

  20. 20

    How to include a namespace in JsonResult

  21. 21

    How do I make non-templated typedefs in the 'namespace' of a templated class?

  22. 22

    How do I implement vector <vector <Obj>> for Cython (C++ to Python)?

  23. 23

    How do I declare a namespace in JavaScript?

  24. 24

    How do I Deserialize Just a String with Namespace?

  25. 25

    How do i open a repl in a different namespace

  26. 26

    How do I get an attribute with namespace?

  27. 27

    How do I control the namespace of a closure in PHP?

  28. 28

    cython cdef class c method: how to call it from another cython cdef class without python overhead?

  29. 29

    Do I have to include non standard libraries to class definitions in C++?

HotTag

Archive