我创建了一个简单的 hello world 程序,并尝试使用 gcc 执行生成的 C 程序,但无论我做什么,我都会得到一大堆 undefined reference 。
SO 上有很多类似的问题,但他们都说要安装 python-dev(或它的某些变体)或添加用于链接和加载库的标志,但我已经尝试了这两个选项。
你好.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”替换头文件仍然会导致 undefined reference 。我正在运行 Ubuntu Linux 3.13.0-73-generic。
如何让我的 cython 代码使用 gcc 进行编译?
谢谢。
编辑
找到/usr/include/-name Python.h 输出:
/usr/include/python2.7/Python.h
/usr/include/python3.4m/Python.h
找到/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)
最佳答案
最好使用 python2-config 来获取适当的标志以传递给您的编译器:
gcc `python2-config --cflags --ldflags` hello.c -o hello
因为您是在一次调用中进行编译和链接,所以您需要将 --cflags 和 --ldflags 传递给 python2-config 。
关于python - 安装 python-dev 和链接库后,Cython 中的 Hello World 程序因 gcc 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34637319/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我打算为ruby脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(