gpt4 book ai didi

c++ - 使用 C 头文件错误编译 Cython

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:52:29 25 4
gpt4 key购买 nike

所以我正在尝试用 Cython 包装一些 C 代码。我阅读了有关执行此操作的应用 Cython 教程(12),但这些教程并没有说明如何用 Cython 包装代码后如何编译代码,因此我有一个错误说它可以'找不到我的 C 代码。

首先,我的 cython 脚本(“calcRMSD.pyx”):

import numpy as np
cimport numpy as np

cdef extern from "rsmd.h":
double rmsd(int n, double* x, double* y)

#rest of the code ommited

我试图包装的 C 代码(“rmsd.h”):

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

extern "C" {
// svd from lapack
void dgesvd_(char*,char*,int*,int*,double*,int*,double*,double*,int*,double*,
int*,double*,int*,int*);
}

double rmsd(int n, double* x, double* y)
{
//code omitted
}

设置.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy as np


setup(
ext_modules = cythonize([Extension("calcRMSD",
sources = ["calcRMSD.pyx"],
include_dirs = [np.get_include()],
libraries = ["dgesvd"]
#extra_compile_args = ["-I."],
#extra_link_args = ["-L./usr/lib/liblapack.dylib"]
)])

)

我的错误:

calcRMSD.c:269:10: fatal error: 'rsmd.h' file not found
#include "rsmd.h"

我读了这个堆栈溢出线程 Using Cython To Link Python To A Shared Library

但遵循它给了我不同的错误。如果我尝试将 rmsd.h 放入源中,它会说它无法识别文件类型。

How to link custom C (which itself needs special linking options to compile) with Cython?

这看起来很有希望,但我不确定如何使用它。

请帮忙!

最佳答案

首先它必须找到包含文件rsmd.h。您需要将可以找到此 header 的路径添加到 include_dirs 参数中。关于丢失文件的错误应该消失。

然后您还需要包含通过编译该 C 代码获得的库。如果是 librsmd.a,您可以将 'rsmd' 添加到 libraries 参数。此外,您可能需要一个 library_dirs 参数,其中包含可以找到该库的路径。

关于c++ - 使用 C 头文件错误编译 Cython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31276470/

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