【问题标题】:Cython with C++ | Compilation error (C++ header not found) | macOSCython 与 C++ |编译错误(未找到 C++ 头文件)|苹果系统
【发布时间】: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 &lt;iostream&gt; 添加到cauchy.h。或者#include &lt;iosfwd&gt;。您可能需要将#include &lt;iostream&gt; 添加到cs_test.cpp。你应该避免using namespace std。相反,请使用std::coutstd::endl 等。
  • @jww :我试过 #include &lt;iostream&gt; 。我收到相同的错误消息。
  • 您是否尝试过在setup.py 文件中指定language="c++"(根据文档:cython.readthedocs.io/en/latest/src/userguide/…
  • @UnholySheep :您推荐给我的文档有效。目前,我正在尝试一种变体,它工作得非常好。感谢您的帮助。
  • 请让您的标题描述问题,而不仅仅是列出技术。

标签: python c++ macos cython cythonize


【解决方案1】:

要让 Cython 使用 distutils 生成和编译 C++ 代码,你只需要传递选项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 上明确说明了这一点。

【讨论】:

    【解决方案2】:

    这是对我有用的设置文件。我还重命名了 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,)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-24
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多