Cython 与 C 接口
Cython Interfacing with C
嗨,我是 Cython 的新手,我想与我现有的 C 代码交互,但我在链接我的程序时遇到了问题。
我之前有一个常规的 cython 模块工作了很多次,现在我想与我的 C 代码进行交互只是为了清楚。
这是我的目录结构:
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()
痕迹
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 Studio17\BuildTools\VC\Tools\MSVC.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 Studio17\BuildTools\VC\Tools\MSVC.16.27023\include -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\ucrt -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\shared -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\um -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\winrt -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\cppwinrt /Tcprogram.c /Fobuild\temp.win-amd64-3.9\Release\program.obj
program.c
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\VC\Tools\MSVC.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 Studio17\BuildTools\VC\Tools\MSVC.16.27023\lib\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\lib.0.17763.0\ucrt\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\lib.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
您需要将 source.c 指定为 program.pyx 文件的来源。一种方法是将 header 注释 # distutils: sources = CSourceCode/source.c
添加到 program.pyx 文件的顶部。您可以在 Configuring the C Build 上查看 Cython 公会以获取更多信息。
嗨,我是 Cython 的新手,我想与我现有的 C 代码交互,但我在链接我的程序时遇到了问题。
我之前有一个常规的 cython 模块工作了很多次,现在我想与我的 C 代码进行交互只是为了清楚。
这是我的目录结构:
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()
痕迹
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 Studio17\BuildTools\VC\Tools\MSVC.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 Studio17\BuildTools\VC\Tools\MSVC.16.27023\include -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\ucrt -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\shared -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\um -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\winrt -IC:\Program Files (x86)\Windows Kits\include.0.17763.0\cppwinrt /Tcprogram.c /Fobuild\temp.win-amd64-3.9\Release\program.obj
program.c
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\VC\Tools\MSVC.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 Studio17\BuildTools\VC\Tools\MSVC.16.27023\lib\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\lib.0.17763.0\ucrt\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\lib.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
您需要将 source.c 指定为 program.pyx 文件的来源。一种方法是将 header 注释 # distutils: sources = CSourceCode/source.c
添加到 program.pyx 文件的顶部。您可以在 Configuring the C Build 上查看 Cython 公会以获取更多信息。