当前位置:  开发笔记 > 编程语言 > 正文

在安装python-dev和链接库之后,Cython中的Hello World程序失败了gcc

如何解决《在安装python-dev和链接库之后,Cython中的HelloWorld程序失败了gcc》经验,为你挑选了1个好方法。

我创建了一个简单的hello world程序,并尝试使用gcc执行生成的C程序,但无论我做什么,我都会获得大量未定义的引用.

在SO上有许多类似的问题,但他们都说安装python-dev(或它的一些变体)或添加标志来链接和加载库,但我已经尝试了这两个选项.

hello.py

def main():
    print "Hello, World!"

if __name__ == "__main__":
    main()

我跑:

cython --embed hello.py

然后我尝试在表单中运行gcc的一些变体:

gcc -I /usr/include/python2.7/ -L /usr/lib/python2.7 -lpython2.7 hello.c -o hello

我要么找不到"Python.h"文件,要么出现以下错误信息:

/tmp/ccxOQlAW.o: In function `__Pyx_PyObject_GetAttrStr':
hello.c:(.text+0x95): undefined reference to `PyObject_GetAttr'
/tmp/ccxOQlAW.o: In function `__pyx_pf_5hello_main':
hello.c:(.text+0x149): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x150): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x15b): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_InitCachedConstants':
hello.c:(.text+0x1fd): undefined reference to `PyCode_New'
/tmp/ccxOQlAW.o: In function `inithello':
hello.c:(.text+0x2f4): undefined reference to `PyTuple_New'
hello.c:(.text+0x33d): undefined reference to `PyString_FromStringAndSize'
hello.c:(.text+0x3bd): undefined reference to `Py_InitModule4_64'
hello.c:(.text+0x423): undefined reference to `PyModule_GetDict'
hello.c:(.text+0x478): undefined reference to `PyImport_AddModule'
hello.c:(.text+0x4cd): undefined reference to `PyObject_SetAttrString'
hello.c:(.text+0x53b): undefined reference to `PyObject_SetAttrString'
hello.c:(.text+0x644): undefined reference to `PyDict_SetItem'
hello.c:(.text+0x7c8): undefined reference to `PyMethod_Type'
hello.c:(.text+0x9ac): undefined reference to `PyDict_New'
hello.c:(.text+0x9f8): undefined reference to `PyDict_SetItem'
hello.c:(.text+0xb79): undefined reference to `PyErr_Occurred'
hello.c:(.text+0xb85): undefined reference to `PyExc_ImportError'
hello.c:(.text+0xb92): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_FetchCommonType':
hello.c:(.text+0xbb3): undefined reference to `PyImport_AddModule'
hello.c:(.text+0xbf1): undefined reference to `PyObject_GetAttrString'
hello.c:(.text+0xc24): undefined reference to `PyExc_TypeError'
hello.c:(.text+0xc36): undefined reference to `PyErr_Format'
hello.c:(.text+0xc63): undefined reference to `PyExc_TypeError'
hello.c:(.text+0xc75): undefined reference to `PyErr_Format'
hello.c:(.text+0xc81): undefined reference to `PyExc_AttributeError'
hello.c:(.text+0xc89): undefined reference to `PyErr_ExceptionMatches'
hello.c:(.text+0xc97): undefined reference to `PyErr_Clear'
hello.c:(.text+0xca3): undefined reference to `PyType_Ready'
hello.c:(.text+0xcc4): undefined reference to `PyObject_SetAttrString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_doc':
hello.c:(.text+0xdae): undefined reference to `PyString_FromString'
hello.c:(.text+0xdda): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0xde5): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0xdea): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_doc':
hello.c:(.text+0xe33): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_name':
hello.c:(.text+0xec7): undefined reference to `PyString_InternFromString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_name':
hello.c:(.text+0xf51): undefined reference to `PyExc_TypeError'
hello.c:(.text+0xf5e): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_qualname':
hello.c:(.text+0x103d): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x104a): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_self':
hello.c:(.text+0x10e3): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_dict':
hello.c:(.text+0x1122): undefined reference to `PyDict_New'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_dict':
hello.c:(.text+0x118a): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x1197): undefined reference to `PyErr_SetString'
hello.c:(.text+0x11ca): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x11d7): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_closure':
hello.c:(.text+0x127c): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x1287): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x128c): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_code':
hello.c:(.text+0x12b2): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_defaults':
hello.c:(.text+0x13ba): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o:hello.c:(.text+0x13c4): more undefined references to `_Py_NoneStruct' follow
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_defaults':
hello.c:(.text+0x13e6): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x13f3): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_defaults':
hello.c:(.text+0x14d6): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_kwdefaults':
hello.c:(.text+0x150d): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x1517): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x1539): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x1546): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_kwdefaults':
hello.c:(.text+0x1629): undefined reference to `_Py_NoneStruct'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_set_annotations':
hello.c:(.text+0x1660): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x168c): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x1699): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_get_annotations':
hello.c:(.text+0x1748): undefined reference to `PyDict_New'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_reduce':
hello.c:(.text+0x17ad): undefined reference to `PyString_FromString'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_New':
hello.c:(.text+0x17da): undefined reference to `_PyObject_GC_New'
hello.c:(.text+0x1957): undefined reference to `PyObject_GC_Track'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_clear':
hello.c:(.text+0x1e72): undefined reference to `PyMem_Free'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_dealloc':
hello.c:(.text+0x1e9d): undefined reference to `PyObject_GC_UnTrack'
hello.c:(.text+0x1eb6): undefined reference to `PyObject_ClearWeakRefs'
hello.c:(.text+0x1ece): undefined reference to `PyObject_GC_Del'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_descr_get':
hello.c:(.text+0x224a): undefined reference to `PyMethod_New'
hello.c:(.text+0x2254): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x2275): undefined reference to `PyMethod_New'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_repr':
hello.c:(.text+0x2293): undefined reference to `PyString_AsString'
hello.c:(.text+0x22a9): undefined reference to `PyString_FromFormat'
/tmp/ccxOQlAW.o: In function `__Pyx_CyFunction_Call':
hello.c:(.text+0x22d6): undefined reference to `PyCFunction_Call'
/tmp/ccxOQlAW.o: In function `__pyx_CyFunction_init':
hello.c:(.text+0x22e7): undefined reference to `PyCFunction_Call'
/tmp/ccxOQlAW.o: In function `__Pyx_GetBuiltinName':
hello.c:(.text+0x2357): undefined reference to `PyExc_NameError'
hello.c:(.text+0x2369): undefined reference to `PyErr_Format'
/tmp/ccxOQlAW.o: In function `__Pyx_GetModuleGlobalName':
hello.c:(.text+0x2391): undefined reference to `PyDict_GetItem'
/tmp/ccxOQlAW.o: In function `__Pyx_PyBytes_Equals':
hello.c:(.text+0x2409): undefined reference to `PyString_Type'
hello.c:(.text+0x241a): undefined reference to `PyString_Type'
hello.c:(.text+0x24e2): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x24f3): undefined reference to `PyString_Type'
hello.c:(.text+0x2513): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x2524): undefined reference to `PyString_Type'
hello.c:(.text+0x254f): undefined reference to `PyObject_RichCompare'
/tmp/ccxOQlAW.o: In function `__Pyx_PyObject_Call':
hello.c:(.text+0x25f6): undefined reference to `PyObject_Call'
hello.c:(.text+0x2602): undefined reference to `_PyThreadState_Current'
hello.c:(.text+0x2614): undefined reference to `_Py_CheckRecursionLimit'
hello.c:(.text+0x262b): undefined reference to `_Py_CheckRecursiveCall'
hello.c:(.text+0x265f): undefined reference to `_PyThreadState_Current'
hello.c:(.text+0x267d): undefined reference to `PyErr_Occurred'
hello.c:(.text+0x2692): undefined reference to `PyExc_SystemError'
hello.c:(.text+0x269f): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_PyObject_CallMethO':
hello.c:(.text+0x26d8): undefined reference to `_PyThreadState_Current'
hello.c:(.text+0x26ea): undefined reference to `_Py_CheckRecursionLimit'
hello.c:(.text+0x2701): undefined reference to `_Py_CheckRecursiveCall'
hello.c:(.text+0x2734): undefined reference to `_PyThreadState_Current'
hello.c:(.text+0x2752): undefined reference to `PyErr_Occurred'
hello.c:(.text+0x2767): undefined reference to `PyExc_SystemError'
hello.c:(.text+0x2774): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx__PyObject_CallOneArg':
hello.c:(.text+0x2794): undefined reference to `PyTuple_New'
/tmp/ccxOQlAW.o: In function `__Pyx_PyObject_CallOneArg':
hello.c:(.text+0x2840): undefined reference to `PyCFunction_Type'
hello.c:(.text+0x2882): undefined reference to `PyType_IsSubtype'
/tmp/ccxOQlAW.o: In function `__Pyx_PyObject_CallNoArg':
hello.c:(.text+0x28fe): undefined reference to `PyCFunction_Type'
hello.c:(.text+0x2940): undefined reference to `PyType_IsSubtype'
/tmp/ccxOQlAW.o: In function `__pyx_insert_code_object':
hello.c:(.text+0x2ba0): undefined reference to `PyMem_Malloc'
hello.c:(.text+0x2d01): undefined reference to `PyMem_Realloc'
/tmp/ccxOQlAW.o: In function `__Pyx_CreateCodeObjectForTraceback':
hello.c:(.text+0x2e0b): undefined reference to `PyString_FromString'
hello.c:(.text+0x2e41): undefined reference to `PyString_FromFormat'
hello.c:(.text+0x2e53): undefined reference to `PyString_FromString'
hello.c:(.text+0x2ede): undefined reference to `PyCode_New'
/tmp/ccxOQlAW.o: In function `__Pyx_AddTraceback':
hello.c:(.text+0x305c): undefined reference to `_PyThreadState_Current'
hello.c:(.text+0x306d): undefined reference to `PyFrame_New'
hello.c:(.text+0x3090): undefined reference to `PyTraceBack_Here'
/tmp/ccxOQlAW.o: In function `main':
hello.c:(.text+0x3131): undefined reference to `Py_SetProgramName'
hello.c:(.text+0x3136): undefined reference to `Py_Initialize'
hello.c:(.text+0x3154): undefined reference to `PySys_SetArgv'
hello.c:(.text+0x3170): undefined reference to `PyErr_Occurred'
hello.c:(.text+0x317a): undefined reference to `PyErr_Print'
hello.c:(.text+0x317f): undefined reference to `Py_FlushLine'
hello.c:(.text+0x3188): undefined reference to `PyErr_Clear'
hello.c:(.text+0x31ce): undefined reference to `Py_Finalize'
/tmp/ccxOQlAW.o: In function `__Pyx_GetStdout':
hello.c:(.text+0x31e7): undefined reference to `PySys_GetObject'
hello.c:(.text+0x31f9): undefined reference to `PyExc_RuntimeError'
hello.c:(.text+0x3206): undefined reference to `PyErr_SetString'
/tmp/ccxOQlAW.o: In function `__Pyx_Print':
hello.c:(.text+0x326f): undefined reference to `PyFile_SoftSpace'
hello.c:(.text+0x3284): undefined reference to `PyFile_WriteString'
hello.c:(.text+0x32bc): undefined reference to `PyFile_WriteObject'
hello.c:(.text+0x32ea): undefined reference to `PyString_AsString'
hello.c:(.text+0x32fa): undefined reference to `PyString_Size'
hello.c:(.text+0x333c): undefined reference to `PyFile_SoftSpace'
hello.c:(.text+0x3374): undefined reference to `PyFile_WriteString'
hello.c:(.text+0x338b): undefined reference to `PyFile_SoftSpace'
/tmp/ccxOQlAW.o: In function `__Pyx_PrintOne':
hello.c:(.text+0x3453): undefined reference to `PyFile_SoftSpace'
hello.c:(.text+0x3468): undefined reference to `PyFile_WriteString'
hello.c:(.text+0x3486): undefined reference to `PyFile_WriteObject'
hello.c:(.text+0x349d): undefined reference to `PyFile_WriteString'
/tmp/ccxOQlAW.o: In function `__Pyx_check_binary_version':
hello.c:(.text+0x355b): undefined reference to `PyOS_snprintf'
hello.c:(.text+0x3560): undefined reference to `Py_GetVersion'
hello.c:(.text+0x3584): undefined reference to `PyOS_snprintf'
hello.c:(.text+0x35e0): undefined reference to `PyOS_snprintf'
hello.c:(.text+0x35f9): undefined reference to `PyErr_WarnEx'
/tmp/ccxOQlAW.o: In function `__Pyx_InitStrings':
hello.c:(.text+0x3667): undefined reference to `PyUnicodeUCS4_DecodeUTF8'
hello.c:(.text+0x368f): undefined reference to `PyString_InternFromString'
hello.c:(.text+0x36ba): undefined reference to `PyString_FromStringAndSize'
/tmp/ccxOQlAW.o: In function `__Pyx_PyObject_IsTrue':
hello.c:(.text+0x3708): undefined reference to `_Py_TrueStruct'
hello.c:(.text+0x3719): undefined reference to `_Py_ZeroStruct'
hello.c:(.text+0x372c): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x3749): undefined reference to `PyObject_IsTrue'
collect2: error: ld returned 1 exit status

我已经安装了python-dev(及其变体).用"python2.7/header_file"替换头文件仍会导致未定义的引用.我正在运行Ubuntu Linux 3.13.0-73-generic.

如何使用gcc编译我的cython代码?

谢谢.

编辑

find/usr/include/-name Python.h输出:

/usr/include/python2.7/Python.h
/usr/include/python3.4m/Python.h

find/usr/lib*-name libpython2.7.so输出:

/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so

我正在构建一个64位系统,gcc目标是64位( - build = x86_64-linux-gnu --host = x86_64-linux-gnu --target = x86_64-linux-gnu)



1> b4hand..:

最好使用python2-config相应的标志传递给您的编译器:

gcc `python2-config --cflags --ldflags` hello.c -o hello

因为你编译并在单个调用连接,你需要同时通过--cflags--ldflagspython2-config.


不幸的是,那对我没有帮助。我有与TS相同的错误
推荐阅读
mobiledu2402851373
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有