我有一个项目由不同文件夹中的几个 Cython 模块组成:
clibs
File.cpp
File.hpp
module
module/folder
__init__.py
file1.pyx
file1.pxd
__init__.py
file2.pyx
在 module/folder/file1.pxd
我有这样的东西:
cdef extern from "../../clibs/File.hpp":
cdef cppclass MyCppClass:
int _data
MyCppClass(int arg)
cdef class MyPyClass:
cdef MyCppClass* cpp_obj
然后在 module/file2.pyx
中,我 cimport
来自 module/folder/file1.pxd
的声明:
from module.folder.file1 cimport MyCppClass, MyPyClass
现在,当我尝试构建它时,cython 生成了一行
#include "../../clibs/File.hpp"
在读取module/folder/file1.pxd
时,丢进了新生成的module/file2.cpp
中,显然是无效路径!
我该如何解决这个问题?我可以在我的 extern
子句中指定相对于项目根目录的路径吗?
最佳答案
我的(临时)解决方案是在每个 python 文件夹中创建一个指向 clibs
目录的符号链接(symbolic link)。
为了避免这成为版本控制的问题,我在 setup.py
的顶部和底部添加了以下内容:
# at the top:
import os
pwd = os.getcwd()
folders = [
"module",
"module/folder"
]
for f in folders:
os.system("ln -s " + pwd + "/clibs " + f + "/clibs")
try:
# setup commands...
finally:
# used here so that if setup raises a compilation exception, we
# still tidy up:
for f in folders:
os.system("rm " + f + "/clibs")
注意:您需要完整的pwd
- 如果您只是指定本地文件夹名称,这就是存储在符号链接(symbolic link)中的所有内容,并且您得到一个 ELOOP 太多的符号链接(symbolic link)错误。
我仍然想要一个更好的解决方案,这只是一个肮脏的 hack,只有当 ln -s
和 rm
存在时才有效!如果没有发布其他内容,我最终会将其标记为“已接受”,但这根本没有必要。
更新:另一个错误是,当代码在网络文件系统上时(例如在 VirtualBox 中的共享文件夹中),您不允许在 guest 上创建符号链接(symbolic link) - 非常令人沮丧!
关于c++ - Cython/C++ - 共享 PXD 文件中的头文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15451740/