0

目前我正试图让 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'不幸的是)

提前感谢您的回答!

4

1 回答 1

1

对于您的第一个问题:如果您的枚举和函数定义为

//YYY.h
namespace Foo{
  enum Bar {bar1, bar2};
  void do_it(Bar bar);
}

那么您需要在 cython 中导入枚举值以及枚举类型名称,它应该如下所示:

cdef extern from 'XXX/YYY.h' namespace 'Foo':
    ctypedef enum Bar:
      bar1
      bar2
    cpdef void do_it(Bar a)

然后可以通过do_it(bar1)do_it(bar2)在您的 cython 代码中的某处调用它。实际上,我前段时间在 SO 上从这个问题中学到了这一点,所以你可以考虑对其进行投票。

第二个问题是您需要链接到 juce 库(现在您只使用包含),为此您需要将以下内容添加到您的扩展设置中:

Extension('testb', ['src/JUCE/testb.pyx'], ...
          libraries=[juce],#here the right name on your system
          library_dirs=[PATH_TO_JUCE_LIB_ON_YOUR_SYSTEM]
         }

PS:如果您不喜欢 inplace-option 的结果,请查阅distutils-documentation以获取替代方案。

编辑:JUCE 有一些需要提供给链接器的进一步依赖项。我认为最好的方法是为您的系统构建一个 c++ 示例(例如HelloWorld)并查看需要哪些库。

通过查看Linux 上的 Makefile,似乎至少需要以下库:freetype2 libcurl x11 xext xinerama webkit2gtk-4.0 gtk+-x11-3.0. 也许你甚至需要先安装它们,我不认为它们在每个系统上都是默认的。

然后您的设置应如下所示:

Extension('testb', ['src/JUCE/testb.pyx'], ...
          libraries=['juce', 'x11', 'freetype2', ...], #all needed libraries
          library_dirs=[PATH_TO_JUCE, '/usr/X11R6/lib', '/usr/lib', ...] #all needed library paths
         }
于 2017-07-31T04:16:28.700 回答