编译错误gcc与Python.h [英] compile error gcc with Python.h

查看:164
本文介绍了编译错误gcc与Python.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c ++中嵌入python,我已经玩了一些示例代码。我正在使用boost python解释器,它工作正常,但现在我似乎不能编译一些使用Python.h的c ++代码。我得到一个错误,似乎是库没有正确引用(这段代码应该工作,因为它直接复制 http://www.codeproject.com/Articles/11805/Embedding-Python-in-CC-Part-I )。我试过许多标志进行编译。任何帮助将非常感激!谢谢:)

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

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

以下是c ++代码

// 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;
}

以下是python脚本:

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


推荐答案

您需要使用 -lpython2.6

编译器无法找到在 libpythonX.Y.so 中定义的python函数。要让它使用该库,您需要添加 -lpythonX.Y 。因为你的Python版本是2.6,你需要使用 -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.

事实上,你得到的东西像(。text + 0xf00)告诉你,这是一个链接器问题,这意味着你的代码本身是好的。问题只是一些功能没有完全定义。这意味着,编译器在编译(从头)时知道原型(即返回类型和参数值),但它不知道实际代码在哪里。这是由链接器计算出来的,它不能知道在哪里它会找到必要的功能。

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.

这篇关于编译错误gcc与Python.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆