What does this ImportError mean when importing my c++ module?
<p>I've been working on writing a Python module in C++. I have a C++ <a href="https://github.com/justinrixx/Asteroids2.0/blob/master/gameNNRunner.cpp" rel="nofollow">program</a> that can run on its own. It works great, but I thought it would be better if I could actually call it like a function from Python. So I took my best go at it, and it builds and installs. Here's the code for my module (called nnrunner.cpp):</p>

<pre><code>#include &lt;Python.h&gt;
#include &lt;vector&gt;
#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&lt;int&gt; topology;
    topology.push_back(20);

    Network net(31, 4, topology);

    net.fromFile(filename);

    NNAI ai(pGame, net);

    pGame-&gt;setAI(&amp;ai);

    while (!pGame-&gt;isGameOver())
        pGame-&gt;update(NULL);

    return pGame-&gt;getScore();
}

static PyObject *
nnrunner_run(PyObject * self, PyObject * args)
{
    string filename;
    int score;
    if (!PyArg_ParseTuple(args, "s", &amp;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(&amp;nnrunnermodule);
    if (m == NULL)
        return NULL;

    return m;
}
</code></pre>

<p>And my build script (called setup.py):</p>

<pre><code>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])
</code></pre>

<p>It has to compile with <code>-lglut -lGL -lGLU</code> due to a dependency, but it doesn't actually have any UI.
I can compile it and install it (<code>python setup.py build</code>, <code>python setup.py install</code>) but when I try to import it, I get errors:</p>

<pre><code>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.
&gt;&gt;&gt; import nnrunner
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ImportError: /home/justin/anaconda3/lib/python3.5/site-packages/nnrunner.cpython-35m-x86_64-linux-gnu.so: undefined symbol: _ZTVNSt7__cxx1115basic_stringbufIcSt11char_traitsIcESaIcEEE
&gt;&gt;&gt; 
</code></pre>

<p>Could somebody point me in the direction of documentation about this? This is the first time I've tried to make a Python module in C++.</p>

<p>Most likely it means that you're importing a shared library that has a binary interface not compatible with your Python distribution. </p>

<p>So in your case: You have a 64-bit Python, and you're importing a 32-bit library, or vice-versa. (Or as suggested in a comment, a different compiler is used).</p>

python c++