简体   繁体   中英

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM