0

我正在使用python 3.4并且我将包装一些 cpp 文件,这些文件使用一些新的 c++ 语言功能以及 openmp,但是,我在让它工作时遇到了一些麻烦。我已经使用 brew 安装了 gcc5,我可以很好地编译我的 cpp 文件。当我尝试将它们包装到 pyx 文件中时,会出现编译器错误。这是我的setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
import os

os.environ["CC"] = "/usr/local/Cellar/gcc/5.2.0/bin/g++-5"
os.environ["CXX"] = "/usr/local/Cellar/gcc/5.2.0/bin/g++-5"

modules = [Extension("constellation",
                     ["constellation.pyx"],
                     language="c++",
                     extra_compile_args=["-std=c++1y"])]

for e in modules:
    e.cython_directives = {"embedsignature": True}

setup(name="constellation",
      cmdclass={"build_ext": build_ext},
      ext_modules=modules)

跑步

python3 setup.py build_ext --inplace

我收到很多这样的错误:

cc1plus: warning: command line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++
/var/folders/33/vsltc7m51l77bd5qlw3bwxzm0000gn/T//ccuePcPk.s:39:bad register name `%rdi)'
/var/folders/33/vsltc7m51l77bd5qlw3bwxzm0000gn/T//ccuePcPk.s:40:bad register name `%rdi)'
/var/folders/33/vsltc7m51l77bd5qlw3bwxzm0000gn/T//ccuePcPk.s:41:bad register name `%rdi)'
/var/folders/33/vsltc7m51l77bd5qlw3bwxzm0000gn/T//ccuePcPk.s:101:bad register name `%rbx'
...
/var/folders/33/vsltc7m51l77bd5qlw3bwxzm0000gn/T//cca3Gn0U.s:15821:Rest of line ignored. 1st junk character valued 64 (@).
error: command '/usr/local/Cellar/gcc/5.2.0/bin/g++-5' failed with exit status 1

如果我将编译器更改为 normal g++,它可以正常工作并编译Apple LLVM version 6.1.0 (clang-602.0.53)

有谁知道发生了什么?构建一个静态或动态库并包装它会更好吗?如果你不知道,我对 Cython 很陌生。任何意见是极大的赞赏!

4

1 回答 1

1

我设法让它编译。这是我setup.py的万一它可以帮助任何有同样问题的人:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
import os

os.environ["CC"]= "/usr/local/Cellar/gcc/5.2.0/bin/g++-5"
os.environ["CXX"]= "/usr/local/Cellar/gcc/5.2.0/bin/g++-5"
os.environ["MACOSX_DEPLOYMENT_TARGET"]= "10.10"
os.environ["ARCHFLAGS"]= "-arch x86_64"
os.environ["CFLAGS"]= (
    '-I/Users/Thomas/Documents/workspace/Quantum/Symengine '
    '-I/Users/Thomas/Documents/workspace/Quantum/Symengine/symengine '
    '-I/Users/Thomas/Documents/workspace/Quantum/Symengine/symengine/teuchos '
    '-I/Users/Thomas/Documents/workspace/Quantum/SymCell '
    '-I/Users/Thomas/googletest/googletest/include '
    '-I/Users/Thomas/Documents/workspace/Quantum/QuantumCell '
    '\"-I/Users/Thomas/Documents/workspace/Quantum/Quantum CPP\" '
)

extensions = [
    Extension("constellation",
        sources=["constellation.pyx"],
        language="c++",
        extra_compile_args=["-std=c++1y","-fopenmp"],
        extra_link_args=["-fopenmp"])]

setup(
    name = "constellation",
    ext_modules = cythonize(extensions)
)

看起来我的 cython 设置不正确,我需要手动设置一些环境变量。特别是,编译器-arch i386出于某种原因正在添加。明确设置ARCHFLAGS摆脱它。看起来我也可以在 c++ 代码中使用 openmp,这太棒了!越过我的手指...

于 2015-10-17T06:46:28.263 回答