Reputation: 175
Hi I am new to Cython and I would like to interface with my existing C code and I am having trouble linking my program.
I have had a regular cython module working plenty of times before now I would like to interface with my C code just to be clear.
Here is what my directory structure look like:
Example <-- Master directory opened from here
CSourceCode
source.c
source.h
cprogram.pxd
program.c
program.pyx
setup.py
testing_interface.py
Setup.py
from setuptools import setup, Extension
from Cython.Build import cythonize
setup (
ext_modules=cythonize([Extension("program", ["program.pyx"])])
)
cprogram.pxd
cdef extern from "CSourceCode/source.h":
void hello()
source.c
#include <stdio.h>
int main(void)
{
return 0;
}
void hello()
{
printf("HELLO FROM C");
}
source.h
#ifndef _SOURCE
#define _SOURCE "source.h"
void hello();
#endif
program.pyx
cimport cprogram
def hello():
cprogram.hello()
testing_interface.py
import program
program.hello()
trace
python setup.py build_ext -i
Compiling program.pyx because it changed.
[1/1] Cythonizing program.pyx
C:\Users\bmxfi\AppData\Local\Programs\Python\Python39\lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\bmxfi\Desktop\code\CPython\CInterfacing\Example\program.pyx
tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'program' extension
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -I. -IC:\Users\bmxfi\AppData\Local\Programs\Python\Python39\include -IC:\Users\bmxfi\AppData\Local\Programs\Python\Python39\include -IC:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt /Tcprogram.c /Fobuild\temp.win-amd64-3.9\Release\program.obj
program.c
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Users\bmxfi\AppData\Local\Programs\Python\Python39\libs /LIBPATH:C:\Users\bmxfi\AppData\Local\Programs\Python\Python39\PCbuild\amd64 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\lib\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\ucrt\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17763.0\um\x64 /EXPORT:PyInit_program build\temp.win-amd64-3.9\Release\program.obj /OUT:build\lib.win-amd64-3.9\program.cp39-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.9\Release\program.cp39-win_amd64.lib
Creating library build\temp.win-amd64-3.9\Release\program.cp39-win_amd64.lib and object build\temp.win-amd64-3.9\Release\program.cp39-win_amd64.exp
program.obj : error LNK2001: unresolved external symbol hello
build\lib.win-amd64-3.9\program.cp39-win_amd64.pyd : fatal error LNK1120: 1 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\link.exe' failed with exit code 1120
Upvotes: 4
Views: 656
Reputation: 313
You need to specify the source.c as a source for the program.pyx file. One way to do so would be to add the header comment # distutils: sources = CSourceCode/source.c
to top of your program.pyx file. You can see the Cython guild on Configuring the C Build for more information.
Upvotes: 3