1

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.

4
  • Does this answer your question? C++ Fatal Error LNK1120: 1 unresolved externals Commented Jan 11, 2020 at 4:25
  • Not really. Although perhaps it does and I'm too dumb to realize it. Commented Jan 11, 2020 at 4:34
  • I've tried (i) not doing anything and simply calling the python command at the end of the post and (ii) compile it first with gcc -c cppFoo.cpp and then compile the cython stuff. However, both give me the same error. Commented Jan 11, 2020 at 4:47
  • Glad you got it working... cheers! Commented Jan 11, 2020 at 4:57

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.