3

I am trying to embed python in c++ and I have been playing with some sample code for a while. I was working with boost python interpreter and it works okay but right now I cannot seem to compile some c++ code that uses Python.h. I get an error that seems to be the library is not referenced correctly (this code is supposed to work as it is copied straight off of http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I). I have tried many flags for compiling. Any help will be much appreciated! Thanks :)

Below is the one example and the error I received:

g++ -Wall -o call_function call_function.c

call_function.c: In function âint main(int, char**)â:
call_function.c:61:56: warning: format â%dâ expects argument of type âintâ, but argument 2 has type âlong intâ [-Wformat]
/tmp/ccAUMMHm.o: In function `main':
call_function.c:(.text+0x2a): undefined reference to `Py_Initialize'
call_function.c:(.text+0x3d): undefined reference to `PyString_FromString'
call_function.c:(.text+0x4d): undefined reference to `PyImport_Import'
call_function.c:(.text+0x5d): undefined reference to `PyModule_GetDict'
call_function.c:(.text+0x7b): undefined reference to `PyDict_GetItemString'
call_function.c:(.text+0x8b): undefined reference to `PyCallable_Check'
call_function.c:(.text+0xb2): undefined reference to `PyTuple_New'
call_function.c:(.text+0xe5): undefined reference to `PyInt_FromLong'
call_function.c:(.text+0xf5): undefined reference to `PyErr_Print'
call_function.c:(.text+0x118): undefined reference to `PyTuple_SetItem'
call_function.c:(.text+0x13f): undefined reference to `PyObject_CallObject'
call_function.c:(.text+0x195): undefined reference to `PyObject_CallObject'
call_function.c:(.text+0x1ac): undefined reference to `PyInt_AsLong'
call_function.c:(.text+0x1fd): undefined reference to `PyErr_Print'
call_function.c:(.text+0x204): undefined reference to `PyErr_Print'
call_function.c:(.text+0x279): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status

The following is the c++ code

// call_function.c - A sample of calling python functions from C code
//
#include "/usr/include/python2.6/Python.h"

int main(int argc, char *argv[])
{
    int i;
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
    if (argc < 3)
    {
        printf("Usage: exe_name python_source function_name\n");
        return 1;
    }
    // Initialize the Python Interpreter
    Py_Initialize();
    // Build the name object
    pName = PyString_FromString(argv[1]);
    // Load the module object
    pModule = PyImport_Import(pName);
    // pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);
    // pFunc is also a borrowed reference
    pFunc = PyDict_GetItemString(pDict, argv[2]);
    if (PyCallable_Check(pFunc))
    {
        // Prepare the argument list for the call
        if( argc > 3 )
        {
                pArgs = PyTuple_New(argc - 3);
                for (i = 0; i < argc - 3; i++)
                {
                    pValue = PyInt_FromLong(atoi(argv[i + 3]));
                    if (!pValue)
                    {
                        PyErr_Print();
                        return 1;
                    }
                    PyTuple_SetItem(pArgs, i, pValue);
                }
                pValue = PyObject_CallObject(pFunc, pArgs);
                if (pArgs != NULL)
                {
                    Py_DECREF(pArgs);
                }
        } else
        {
                pValue = PyObject_CallObject(pFunc, NULL);
        }
        if (pValue != NULL)
        {
            printf("Return of call : %d\n", PyInt_AsLong(pValue));
            Py_DECREF(pValue);
        }
        else
        {
            PyErr_Print();
        }
    } else
    {
        PyErr_Print();
    }

    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pName);
    // Finish the Python Interpreter
    Py_Finalize();
    return 0;
}

The following is the python script:

'''py_function.py - Python source designed to '''
'''demonstrate the use of python embedding'''

def multiply():
    c = 12345*6789
    print 'The result of 12345 x 6789 :', c
    return c

1 Answer 1

8

You need to compile with -lpython2.6.

The compiler is unable to find the python functions, which are defined in libpythonX.Y.so. To tell it to use that library, you need to add -lpythonX.Y. As your Python version is 2.6, you need to use -lpython2.6.

The fact that you're getting things like (.text+0xf00) tells you that this is a linker problem, which means that your code itself is fine. The problem is just that some functions are not fully defined. This means, the compiler knew the prototype (that is, return type and argument values) while compiling (from the header), but it doesn't know where the actual code is. It is up to the linker to figure that out, and it cannot know by magic where it'll find the neccessary functions.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks for the help @Jonas Wielicki. I knew I had to load python2.6 but I thought by entering the path that should be fine. I can not seem to find pythonX.Y. I actually tried compiling by : g++ -Wall -I/usr/include/python2.6 -lpython2.6 -lpythonX.Y -o call_function call_function.c but the error is it cannot find /usr/bin/ld : -lpythonX.Y
-lpythonX.Y was only an example. Don't add it to your command line. X and Y are placeholders for the python version number. Also, don't mix up the sources (for which you add the pathes using -I…) and the dynamic libraries, which you have to pass to the linker using -l. Maybe you want to read up on the difference between compilers and linkers.
thanks a lot for your help Jonas. Can you explain a little bit about what -I does and -l does? I now understand that -l adds the library to the linker if I am not mistaken but what does -I flag do?
Hmm...I have -lpython2.7 in my flags already but I have an identical error.
@PatrizioBertoni A quick search in the docs for Python 2.7 vs. 3.5 show that PyInt_AsLong does not exist in 3.5.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.