为什么我可以编译为C而不是Cython在Mac OS X上的C++

2024-09-30 01:33:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找出如何使用Cython在Python中使用C/C++代码。我可以得到以下作为C代码的示例:

总和h:

#ifndef MY_SUM_H_
#define MY_SUM_H_

int my_sum(int a, int b);

#endif

求和c:

^{pr2}$

测试.pyx:

cdef extern from "my_sum.h":
    cdef int my_sum(int a, int b)

cpdef sum_wrap(int a, int b):
    return my_sum(a, b)

设置.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("test", ["test.pyx", "my_sum.c"], language = "c")]

setup(cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules)

但是,如果我尝试将其测试为C++代码,它就会失败。我将sum.c重命名为sum.cpp,并在中将language更改为{}设置.py. 之后,它看起来像这样:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("test", ["test.pyx", "my_sum.cpp"], language = "c++")]

setup(cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules)

够了吧?它会产生以下错误:

$ python setup.py build_ext --inplace
running build_ext
cythoning test.pyx to test.cpp
/Users/jensrenders/miniconda3/lib/python3.7/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /Users/jensrenders/Dropbox/cython_demo/test.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
building 'test' extension
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include/python3.7m -c test.cpp -o build/temp.macosx-10.7-x86_64-3.7/test.o
warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the command line to use the libc++ standard
      library instead [-Wstdlibcxx-not-found]
1 warning generated.
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include/python3.7m -c my_sum.cpp -o build/temp.macosx-10.7-x86_64-3.7/my_sum.o
warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the command line to use the libc++ standard
      library instead [-Wstdlibcxx-not-found]
1 warning generated.
g++ -bundle -undefined dynamic_lookup -L/Users/jensrenders/miniconda3/lib -arch x86_64 -L/Users/jensrenders/miniconda3/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.7-x86_64-3.7/test.o build/temp.macosx-10.7-x86_64-3.7/my_sum.o -o /Users/jensrenders/Dropbox/cython_demo/test.cpython-37m-darwin.so
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'g++' failed with exit status 1

我确实得到一个test.cpp文件作为输出,但没有共享对象。 是什么原因造成的,如何解决?在

谢谢你的帮助!在


编辑: 正如MaximEgorushkin指出的,Cython试图用^ {CD6}}编译C++文件是很奇怪的。我可以通过将 os.environ["CC"] = "g++"添加到设置.py,但这并不能解决问题:

$ python setup.py build_ext --inplace
running build_ext
building 'test' extension
g++ -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include/python3.7m -c test.cpp -o build/temp.macosx-10.7-x86_64-3.7/test.o
warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the command line to use the libc++ standard
      library instead [-Wstdlibcxx-not-found]
1 warning generated.
g++ -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include -arch x86_64 -I/Users/jensrenders/miniconda3/include/python3.7m -c my_sum.cpp -o build/temp.macosx-10.7-x86_64-3.7/my_sum.o
warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the command line to use the libc++ standard
      library instead [-Wstdlibcxx-not-found]
1 warning generated.
g++ -bundle -undefined dynamic_lookup -L/Users/jensrenders/miniconda3/lib -arch x86_64 -L/Users/jensrenders/miniconda3/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.7-x86_64-3.7/test.o build/temp.macosx-10.7-x86_64-3.7/my_sum.o -o /Users/jensrenders/Dropbox/cython_demo/test.cpython-37m-darwin.so
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'g++' failed with exit status 1

Tags: testbuildincludemylibnotusersext
1条回答
网友
1楼 · 发布于 2024-09-30 01:33:14

这里有一个类似的问题: https://github.com/pandas-dev/pandas/issues/23424

正如他们所建议的,以及输出中的行所示

clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]

添加extra_link_args=["-stdlib=libc++", "-mmacosx-version-min=10.9"]解决了这个问题。setup.py然后如下所示:

^{pr2}$

相关问题 更多 >

    热门问题