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