我有一个 .pxd 文件,其中包含另一个 c 头文件中的变量声明,可以从以下代码中理解:
cdef extern from "parameters.h":
ctypedef struct mm_np:
int nlay
double n1
double n2[3]
double d[3]
double n3
int lut
当文件 parameters.h 位于运行 cythonizing 脚本的同一文件夹中时 - 一切都很完美。
问题: 当文件“parameters.h”位于不同的目录而不是 .pxd 文件所在的目录时,如何执行相同的包含?
我尝试将这段代码的第一行替换为
cdef extern from "/liboptv/include/parameters.h":
那是行不通的,当我进行 cythonize 时,我收到一条消息,指出找不到该文件:
fatal error: /liboptv/include/parameters.h: No such file or directory
谢谢。
最佳答案
如上所述,以其中一种方式包含 header 目录的路径会更可靠:
-I
编译器选项include_dirs
setuptools 参数INCLUDE
环境变量
您还可以使用来自包含在上述选项之一中的一个路径的相对导入。例如,如果您想要包含 header :
/usr/include/test/soft1/mod1/header1.h
/usr/include/test/soft1/mod2/header2.h
/usr/include/test/soft1/mod3/header3.h
您可以添加包含所有这些路径的子文件夹:
gcc ... -I/usr/include/test/soft1
并且在 Cython 中使用相对导入:
cdef extern from "mod1/header1.h":
...
cdef extern from "mod2/header1.h":
等等。
关于python - 在 Cython 中从不同目录导入文件的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28972164/