foreignvol
foreignvol

Reputation: 701

Basic C++ function to Python via Cython

I have a very basic setup to experiment on how to expose a C++ function to Python via Cython. However, I keep getting the following error. What am I missing?

foo.obj : error LNK2001: unresolved external symbol cppFoo
C:\git\cythonTest\foo.cp38-win_amd64.pyd : fatal error LNK1120: 1 unresolved externals

cppFoo.h

#ifndef FOO_H
#define FOO_H

double cppFoo(double x, int y);

#endif

cppFoo.cpp

#include "cppFoo.h"

double cppFoo(double x, int y)
{
    return 2 * x + y;
}

cfoo.pxd

cdef extern from "cppFoo.h":
    double cppFoo(double x, int y)

foo.pyx

from cfoo cimport cppFoo

def pyFoo(double x, int y):
    return cppFoo(x, y)

setup.py

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

setup(ext_modules=cythonize("foo.pyx"), requires=['Cython'])

Running with python setup.py clean build_ext --inplace.

Upvotes: 1

Views: 192

Answers (1)

foreignvol
foreignvol

Reputation: 701

I found a solution. cppFoo.cpp was not being recognized as a source file. Adding the following line at the top of foo.pyx was enough.

# distutils: sources = cppFoo.cpp

Upvotes: 1

Related Questions