我正在尝试创建一个包含简单 c 函数的源代码发行版。这是我拥有的文件。
# file setup.py
from setuptools import setup
setup(
name="example",
version="0.1",
py_modules=["example", "build"], # If i dont include build.py, it will not find it.
cffi_modules=["build.py:ffibuilder"],
install_requires=["cffi"],
setup_requires=["cffi"],
)
# file "build.py"
from cffi import FFI
ffibuilder = FFI()
SOURCE = """
#include "factorial.h"
"""
ffibuilder.cdef(
"""
long long int factorial(int n);
"""
)
ffibuilder.set_source(
module_name="_example",
source=SOURCE,
sources=["factorial.c"],
include_dirs=["."],
library_dirs=["."],
)
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
// file "factorial.c"
#include "factorial.h"
long long int factorial(int n)
{
long long int result = 1;
int i;
for (i = 1; i <= n; i++)
result *= i;
return result;
}
// file "factorial.h"
long long int factorial(int n);
使用这些文件我运行命令
python setup.py sdist
这会生成文件“dist xample-0.1.tar.gz”。当我尝试使用
安装它时pip install example-0.1.tar.gz
我明白了
build\temp.win-amd64-3.9\Release\_example.c(570): fatal error C1083: Cannot open include file: 'factorial.h': No such file or directory
那么如何在源代码分发中包含头文件?
除了
package_data
,您还可以按照 4 使用
MANIFEST.in
文件。构建 C 和 C++ 扩展。
在某些情况下,源代码分发中需要包含其他文件;这是通过
文件完成的;有关详细信息,请参阅指定要分发的文件。MANIFEST.in
MANIFEST.in
中,它们包含指定文件夹中的所有文件,其中包含头文件。
在setup.py文件中添加setup函数package_data={"": ["*.h"]}。这将包括源代码分发中的所有头文件。
setup(
name="example",
version="0.1",
py_modules=["example", "build"], # If i dont include build.py, it will not find it.
cffi_modules=["build.py:ffibuilder"],
install_requires=["cffi"],
setup_requires=["cffi"],
package_data={"": ["*.h"]} # <- This line
)