【问题标题】:Cython Interfacing with CCython 与 C 接口
【发布时间】:2021-07-17 12:37:33
【问题描述】:

您好,我是 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 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

【问题讨论】:

标签: python c cython cythonize


【解决方案1】:

您需要将 source.c 指定为 program.pyx 文件的源。一种方法是将标题注释 # distutils: sources = CSourceCode/source.c 添加到您的 program.pyx 文件的顶部。您可以在Configuring the C Build 上查看 Cython 公会以了解更多信息。

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 2023-03-14
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2020-04-20
    • 2010-09-06
    • 1970-01-01
    相关资源
    最近更新 更多