Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:25:21+00:00 2026-06-16T03:25:21+00:00

I want to make available one of my c++ classes as Python module. The

  • 0

I want to make available one of my c++ classes as Python module. The class is declared in a header Foo.h and implemented in a .cpp Foo.cpp.
(g++-4.5, Ubuntu x86_64). It’s a very very simple class:

Foo.cpp:

Foo::Foo() : alfa(1.0), beta(1)
{

}

Foo::~Foo()
{
}

Foo.h:

 class Foo
 {
 public:

  Foo()
  Foo(const Foo& orig);
  ~Foo();
  double alfa;
  int beta; 
 };

I created a setup.py as shown in Cython tutorial:

setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'MyDemo',
  ext_modules=[
    Extension("Foo"
          sources=["Foo.pyx"], 
          include_dirs=[".","../eigen/"],
          language="c++"),
    ],
  cmdclass = {'build_ext': build_ext},
)

and followed the instruction of the cython tutorial to write my Foo.pyx cython module:

Foo.pyx

cdef extern from "Foo.h":
    ctypedef struct c_Foo "Foo":
        double alfa
    c_Foo *new_Foo "new Foo" ()
    void del_Foo "delete" (c_Foo *myfoo)

cdef class Foo:
    cdef c_Foo *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self):
         self.thisptr = new_Foo()
    def __dealloc__(self):
         del_Foo(self.thisptr)

I compile it with the following command:
python setup.py build_ext --inplace

running build_ext
skipping 'Foo.cpp' Cython extension (up-to-date)
building 'Foo extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I. -I../eigen/ -I/usr/include/python2.6 -c Foo.cpp -o build/temp.linux-x86_64-2.6/Foo.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-x86_64-2.6/Foo.o -o /home/linello/prova/Foo.so

Now the Foo.so shared library object is created but when I want to import it from python, I get:

 >>> import Foo
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ImportError: ./Foo.so: undefined symbol: _ZN4FooD1Ev
>>> 

I think that _ZN4FooD1Ev is the mangled name of the constructor of Foo but don’t understand how is missing the symbol.

I really can’t understand what symbol is missing from the shared object file.
And as second point, after the python setup.py build_ext --inplace command, my Foo.cpp file is messed up and contains the cythonized version.

How is possible to rename the cythonized file in another format (for example .cxx) and avoid that linker error?

I then modified the Foo.pyx in pFoo.pyx and consequently modified the setup.py, now after the setup command I have the cythonized version of pFoo.pyx in Foo.cxx but when I try to import I get the

ImportError: dynamic module does not define init function (initpyFoo)

What is wrong with my setup and how is possible to solve my problems?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:25:22+00:00Added an answer on June 16, 2026 at 3:25 am

    I suggest you use a different name for your cython module, e.g. cFoo, to avoid the name collision issue:

    from distutils.core import setup
    from Cython.Build import cythonize
    
    setup(ext_modules = cythonize(
               "cFoo.pyx",                 # our Cython source
               sources=["Foo.cpp"],        # additional source file(s)
               language="c++",             # generate C++ code
          ))
    

    To define a C++ class, use the ‘cppclass’ keyword, as below:

    cdef extern from "Foo.h":
        cdef cppclass Foo:
            Foo()
            double alfa
            int beta
    

    You should then be able to access your class like so:

    cdef Foo *foo = new Foo()
    foo.beta = 42
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use google guice to make properties available in all classes of
I want to make video/audio chat(both one-to-one and chatroom) available on my website through
I want to make a single database object available across many python modules. For
I want to make a Python script available as a service on the net.
I have a C extension module for Python and I want to make it
We have an application that we want to make available to the general internet-using
I have a PHP function that I want to make available publically on the
I have these two URL's I want to make available to my users: /Account/Orders
I have a MS SQL view that I want to make available as a
I have a site running in django and want to make it available on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.