私はPythonモジュールをC++で書いています。私はそれ自身で実行できるC++ programを持っています。それは素晴らしいですが、実際にはPythonから関数のように呼び出せればいいと思っていました。だから私はベストを尽くしてビルドしてインストールします。それが原因の依存関係に-lglut -lGL -lGLU
を使用してコンパイルする必要があり私のC++モジュールをインポートするとき、このImportErrorは何を意味しますか?
from distutils.core import setup, Extension
module1 = Extension('nnrunner',
sources = ['nnrunner.cpp', 'game.cpp', 'uiDraw.cpp',
'uiInteract.cpp', 'player.cpp', 'ship.cpp', 'network.cpp'],
libraries = ['glut', 'GL', 'GLU'])
setup (name = 'NNRunner',
version = '1.0',
description = 'This is my first package',
ext_modules = [module1])
、しかし:
#include <Python.h>
#include <vector>
#include "game.h"
#include "neuralnetai.h"
using namespace std;
/**************************************************
* This is the actual function that will be called
*************************************************/
static int run(string filename)
{
srand(clock());
Game * pGame = new Game();
vector<int> topology;
topology.push_back(20);
Network net(31, 4, topology);
net.fromFile(filename);
NNAI ai(pGame, net);
pGame->setAI(&ai);
while (!pGame->isGameOver())
pGame->update(NULL);
return pGame->getScore();
}
static PyObject *
nnrunner_run(PyObject * self, PyObject * args)
{
string filename;
int score;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
score = run(filename);
return PyLong_FromLong(score);
}
static PyMethodDef NnrunnerMethods[] = {
{"run", nnrunner_run, METH_VARARGS,
"Run the game and return the score"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef nnrunnermodule = {
PyModuleDef_HEAD_INIT,
"nnrunner", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
NnrunnerMethods
};
PyMODINIT_FUNC
PyInit_nnrunner(void)
{
PyObject *m;
m = PyModule_Create(&nnrunnermodule);
if (m == NULL)
return NULL;
return m;
}
そして(setup.pyと呼ばれる)私のビルドスクリプト:ここに私のモジュールのコードは、(nnrunner.cppと呼ばれる)です実際にはUIはありません。 私はそれをコンパイルしてインストールします(python setup.py build
、python setup.py install
)が、私はそれをインポートしようとすると、私はエラーを取得することができます。
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nnrunner
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /home/justin/anaconda3/lib/python3.5/site-packages/nnrunner.cpython-35m-x86_64-linux-gnu.so: undefined symbol: _ZTVNSt7__cxx1115basic_stringbufIcSt11char_traitsIcESaIcEEE
>>>
誰かがこれについてのドキュメントの方向に私を指すもらえますか? C++でPythonモジュールを作ろうとしたのは初めてです。
それとも別のコンパイラ:
はアナコンダ
libgcc
パッケージをアップグレードすることによってそれを解決しました。これはOS XでCをコンパイルするときによく起こります。 – erip私は64ビットのPythonインタプリタを持っていることを知っています。ですから、わかりやすくするために、私が含める ''はおそらく32ビットか、それとも32ビットのような他のライブラリでしょうか?特定のライブラリが32または64ビットであるかどうかを調べるにはどうすればよいですか?それとも、私のPythonビルドスクリプトが使用するコンパイラとは別のコンパイラでコンパイルされたものですか? –
justinrixx
@justinrixxいいえ、私はあなたが使用しているコンパイラが32ビットのバイナリを生成していると言っています。これは1つの可能性です。もう1つの可能性は、Anaconda実行可能ファイルを生成したコンパイラが、libファイルを生成したコンパイラと同じではないことです。ライブラリをLinuxディストリビューションに付属している標準のPythonにインポートしてみてください。どのLinuxディストリビューションを使用していますか? Debian/Ubuntuの場合は、 'sudo apt-get install python3'でPythonをインストールし、' python3'を実行してモジュールをインポートし、何が起こるかを見てください。 –