Cython 与 C++ |编译错误(未找到 C++ header)|苹果系统

Cython with C++ | Compilation error (C++ header not found) | macOS

我正在尝试在 macOS 中使用 Cython 实现一个非常简单的 C++ 代码。这是我的 C++ 代码的 header(这是一个名为 cs_test.h

的文件
#include<iostream>
void cs_test(int n);

这是我的 C++ 代码(文件名:cs_test.cpp:

#include "cs_test.h"
using namespace std;

int main(){}

void cs_test(int n)
{
    cout << "This is C++ output: " << n << endl;
}

这是我的 pyx 代码(文件名:simulate.pyx

import numpy as np
cimport numpy as np

cdef extern from "./cauchy.h" nogil:
void cs_test(int n)

def sim():
    cs_test(5)

最后,这是我的设置代码 (setup.py)

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

setup(
    ext_modules=cythonize("simulate.pyx"),
    include_dirs=[numpy.get_include()]
)  

上述所有文件都在同一个文件夹中。我 运行 setup.py 使用此命令:

python setup.py build_ext --inplace

而且,我收到以下错误消息:

In file included from simulate.c:502:0:
./cauchy.h:1:19: fatal error: iostream: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

simulate.pyx 中,即使我将行 cdef extern from "./cauchy.h" nogil: 替换为 cdef extern from "cauchy.h" nogil:,我仍然会收到相同的错误消息。我了解有关使用 gcc 的错误消息可能是因为我使用的是 macOS。但是,我不知道如何让代码知道使用 clang++c++

我在这里做错了什么?如果能得到帮助,我将不胜感激。

这是对我有用的安装文件。我还重命名了 C++ 代码的头文件以匹配 C++ 文件的名称。

import numpy
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension(
    name="simulate_cy",
    sources=["simulate.pyx", "cs_test.cpp"],
    # extra_objects=["fc.o"],  # if you compile fc.cpp separately
    include_dirs = [numpy.get_include()],  # .../site-
packages/numpy/core/include
    language="c++",
    # libraries=
    extra_compile_args = ['-O3'],
    # extra_link_args = "...".)
    )]

setup(
    name = 'simulate_cy',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules,)

To make Cython generate and compile C++ code with distutils, you just need to pass the option language="c++":

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

setup(ext_modules = cythonize(
       "rect.pyx",                 # our Cython source
       sources=["Rectangle.cpp"],  # additional source file(s)
       language="c++",             # generate C++ code
  ))

这在the documentation page for using C++ with Cython上有明确说明。