[英]compile error gcc with Python.h
我試圖在c ++中嵌入python,我一直在玩一些示例代碼。 我正在使用boost python解釋器,它工作正常,但現在我似乎無法編譯一些使用Python.h的c ++代碼。 我得到一個錯誤似乎是沒有正確引用庫(這段代碼應該工作,因為它直接從http://www.codeproject.com/Articles/11805/Embedding-Python-in-CC-復制第一部分 )。 我已經嘗試了很多標志進行編譯。 任何幫助都感激不盡! 謝謝 :)
以下是我收到的一個示例和錯誤:
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腳本:
'''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
。
事實上你得到的東西(.text+0xf00)
告訴你這是一個鏈接器問題,這意味着你的代碼本身(.text+0xf00)
問題。 問題在於某些功能尚未完全定義。 這意味着,編譯器在編譯時(從頭部)知道原型(即返回類型和參數值),但它不知道實際代碼的位置。 由鏈接器決定這一點,並且它無法通過魔法知道哪里可以找到必要的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.