【发布时间】:2018-02-26 21:29:23
【问题描述】:
我正在尝试在 macOS 中使用 Cython 实现一个非常简单的 C++ 代码。这是我的 C++ 代码的标题(它是一个名为 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++。
我在这里做错了什么?我将非常感谢您的帮助。
【问题讨论】:
-
将
#include <iostream>添加到cauchy.h。或者#include <iosfwd>。您可能需要将#include <iostream>添加到cs_test.cpp。你应该避免using namespace std。相反,请使用std::cout、std::endl等。 -
@jww :我试过
#include <iostream>。我收到相同的错误消息。 -
您是否尝试过在
setup.py文件中指定language="c++"(根据文档:cython.readthedocs.io/en/latest/src/userguide/…) -
@UnholySheep :您推荐给我的文档有效。目前,我正在尝试一种变体,它工作得非常好。感谢您的帮助。
-
请让您的标题描述问题,而不仅仅是列出技术。
标签: python c++ macos cython cythonize