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

标签 python c++ macos cython cythonize

我正在尝试在 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++

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

最佳答案

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 上有明确说明.

关于python - Cython 与 C++ |编译错误(未找到 C++ header )|苹果系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48997398/

相关文章:

swift - 如何仅为一个特定的修改器更改应用动画?

c++ - 使用 std::variant 作为类成员并应用访问者

c++ - D3D11 : variable number of lights in HLSL

python - 无法导入安装在新 Conda 环境中的包

Python:使用 tarfile 从 TAR 存档中删除文件

c++ - 确定字符串是否包含实数或整数值的最快方法

python - Homebrew 无法安装 postgresql; python 64位错误

python-3.x - 在MacOS上安装virtualenv和virtualenvwrapper

python - 双串转int

python - Python中是否有类似于matlab中的 'which'和 'open'的函数?