gpt4 book ai didi

python - 在 C 代码中找不到库

转载 作者:行者123 更新时间:2023-11-28 19:02:55 25 4
gpt4 key购买 nike

我正在尝试使用 cython 编译模块,但编译器无法在外部 c 文件中找到包含库(我使用 cmath 作为示例,但我想要的其他库如 cstdio, cstdint, cstring 有同样的问题)

一个最小的示例由以下 4 个简单文件组成:

c文件.c:

#include <cmath>

test_pxd.pxd:

cdef extern from "cfile.c":
pass

测试.pyx:

cimport test_pxd

设置.py:

from distutils.core import Extension, setup
from Cython.Build import cythonize

sources = ['test.pyx']

extension = [Extension('test',sources)]

setup(ext_modules=cythonize(extension,force=True))

如果我运行 setup.py:

 python3 setup.py build_ext --inplace

我得到错误:

cfile.c:1:17: fatal error: cmath: No such file or directory

应该注意的是,直接编译 c 文件,例如使用 g++ -c cfile.c,编译器不需要任何额外的链接来找到这些库。

如何让 cython 中的编译器找到 cmath(以及其他,例如 cstdiocstdintcstring) 外部 c 文件中的库?

最佳答案

定义应该针对标题,而不是实现:

cdef extern from "cfile.h":
pass

如果使用 C++ 库,必须将 language='c++' 指令添加到 setuptools/cythonize。

cythonize(<..>,
language="c++",
)

另见 docs .

关于python - 在 C 代码中找不到库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50603694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com