目前我正试图让 JUCE 音频框架在 Cython 上工作。因此,我首先想通过 JUCE 框架显示一个AlertWindow来运行一个小而简单的示例,但目前我似乎遇到了两个小问题: 1. 从 JUCE 框架调用枚举时遇到问题2.我不知道如何包含整个框架进行编译和链接。
我的 setup.py(用“python3 setup.py build_ext --inplace”调用):
# Cython compile instructions
from setuptools import setup
from setuptools.extension import Extension
from Cython.Build import cythonize
compile_args = ['-g', '-std=c++11', '-stdlib=libc++']
extensions = [Extension('testb', ['src/JUCE/testb.pyx'],
extra_compile_args=compile_args,
include_dirs = ["JUCE/modules"],)]
setup(
name='My app',
ext_modules=cythonize(extensions)
)
我的 testb.pyx(问题 1 在这里):
# distutils: language = c++
cdef extern from "JuceLibraryCode/JuceHeader.h" namespace "juce":
cdef cppclass AlertWindow:
AlertWindow(String, String, AlertIconType)
cdef class PyAlertWindow:
cdef AlertWindow *thisptr
def __cinit__(self):
self.thisptr = new AlertWindow("", "", NoIcon) # Don't know how to call the enum right here
def __dealloc__(self):
del self.thisptr
@staticmethod
def showAlertWindow(b):
print("at least this works")
此外,我不断收到这些类型的错误,据我所知,这是由于框架的其余部分没有被编译和包含/链接造成的。我该怎么做?
ImportError:
dlopen(<project root>/build/lib/testb.cpython-36m-darwin.so, 2): Symbol not found: __ZN4juce6StringC1Ev
Referenced from:
<project root>/build/lib/testb.cpython-36m-darwin.so
Expected in: flat namespace
in <project root>/build/lib/testb.cpython-36m-darwin.so
此外,使用 --inplace 标志,所有编译的文件都会转储到我的主文件夹中,这似乎不是很可扩展,尤其是对于较大的框架。如何确保所有 .so 文件最终都包含在内,以便轻松引用?还是有更好的方法来解决这个问题?
好的,所以在@ead 的帮助下,我能够更进一步。代码现在如下所示:
# setup.py
from setuptools import setup
from setuptools.extension import Extension
from Cython.Build import cythonize
compile_args = ['-g', '-std=c++11', '-stdlib=libc++']
extensions = [Extension('testb', ['src/program/testb.pyx'],
extra_compile_args=compile_args,
include_dirs = ["JUCE/modules"],
libraries=["NewProject"], #the name of your library, WHICH MIGHT BE DIFFERENT FROM THE FILENAME!
library_dirs=["src/program/JuceLibraryCode"] #where your library is placed.
)]
setup(
name='My app',
ext_modules=cythonize(extensions)
)
还有我的 Cython 文件:
# testb.pyx
# distutils: language = c++
from libcpp.string cimport string
cdef extern from "JuceLibraryCode/JuceHeader.h":
cdef cppclass AlertWindow:
AlertWindow(String, String, AlertIconType, Component*)
cdef cppclass Component:
Component()
cdef cppclass String:
String(string)
cdef extern from "JuceLibraryCode/JuceHeader.h" namespace "juce::AlertWindow":
ctypedef enum AlertIconType:
NoIcon
QuestionIcon
WarningIcon
InfoIcon
cdef class PyAlertWindow:
cdef AlertWindow *thisptr
def __cinit__(self):
self.thisptr = new AlertWindow(String(""), String(""), AlertIconType(NoIcon), NULL)
def __dealloc__(self):
del self.thisptr
该示例现在可以正常编译。但是,当我导入生成的包时,出现以下错误:
ImportError: dlopen(<project root>/testb.cpython-36m-darwin.so, 2): Symbol not found: _CGAffineTransformIdentity
Referenced from: <project root>/testb.cpython-36m-darwin.so
Expected in: flat namespace
in <project root>/testb.cpython-36m-darwin.so
这似乎与 Cocoa (here)或 CoreGraphics (here)有关(我也相信它是 Cocoa 的继承者)。那么我将如何解决这个问题?我是否需要包含 CoreGraphics 框架,如果需要,如何包含?(只需添加标志-framework CoreGraphics会导致clang: error: unknown argument: '-framework CoreGraphics'不幸的是)
提前感谢您的回答!