題在使用 Armadillo 的 C++ 中創建 Python 擴展時,出現以下錯誤:A) 在 Mac OS Mojave 10.14.4、Python 3.7.5 中:Traceback (most recent call last): File "./py_program.py", line 5, in <module> import cmoduleImportError: dlopen(/Users/angel/.pyenv/versions/3.7.5/lib/python3.7/site-packages/cmodule.cpython-37m-darwin.so, 2): Symbol not found: __ZTWN4arma23arma_rng_cxx11_instanceE Referenced from: /Users/angel/.pyenv/versions/3.7.5/lib/python3.7/site-packages/cmodule.cpython-37m-darwin.so Expected in: flat namespace in /Users/angel/.pyenv/versions/3.7.5/lib/python3.7/site-packages/cmodule.cpython-37m-darwin.soB) 在 Ubuntu 20、Python 3.8.2 中:Traceback (most recent call last): File "./py_program.py", line 5, in <module> import cmoduleImportError: /usr/local/lib/python3.8/dist-packages/cmodule.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN4arma23arma_rng_cxx11_instanceE它們都是由于使用arma::arma_rng::randn<double>(),見下文。我該如何解決?細節我想py_program.py導入在cmodule.cpp. 按照文檔https://docs.python.org/3/extending/extending.html,我有文件py_program.py,setup.py和cmodule.cpp. 它們的內容是:py_program.py#!/usr/bin/env python3"""Import and use cmodule."""import cmodulecmodule.printvol(3.)setup.py"""To install the module defined in cmodule.cpp."""from distutils.core import setup, Extensionsetup(name='cmodule', version='1.0', \ ext_modules=[ Extension( 'cmodule', ['cmodule.cpp'], extra_compile_args=['-std=c++11'], language='c++')], )cmodule.cpp/* Module to be used in Python. All Python stuff follows Sec. 1.8 of https://docs.python.org/3/extending/extending.html */#define PY_SSIZE_T_CLEAN#include <Python.h>#include <armadillo>double f()// Fails at using Armadillo.// The module works if I delete this function.{ double rn_y = arma::arma_rng::randn<double>(); return rn_y;}arma::cx_double g()// Succeeds at using Armadillo.{ arma::cx_double value(0., 1.); return value;}
1 回答

喵喵時光機
TA貢獻1846條經驗 獲得超7個贊
在中,解決問題setup.py的參數:libraries=['armadillo']Extension()
"""To install the module defined in cmodule.cpp."""
from distutils.core import setup, Extension
setup(name='cmodule', version='1.0', \
ext_modules=[
Extension(
'cmodule', ['cmodule.cpp'],
extra_compile_args=['-std=c++11'],
libraries=['armadillo'], // this solves the problem
language='c++')],
)
神奇的是,沒有它,arma::也能正確使用。但不是像arma::arma_rng.
這個解決方案是通用的:其他庫也會出現同樣的問題。實際上,我使用 GNU 科學圖書館 ( ) 復制了相同的內容(并使其工作libraries=['gsl'])。
添加回答
舉報
0/150
提交
取消