Cython:编译独立静态可执行文件

12

我正在尝试编译一个不使用动态加载器的可执行文件(ELF文件)。 我使用 Cython 将Python编译为C:

cython3 -3 test.py --embed

那么

gcc test.c -otest $(pkg-config --libs --cflags python3)

编译C生成的文件。

现在我想编译一个静态可执行文件test.c。通常我使用-static标志,但这里返回了collect2: error: ld returned 1 exit status

我该怎么办?

编辑1:因为正文限制为30000个字符,所以不是完整的消息

一长串警告和错误消息,例如:

...
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x430): undefined reference to `XML_SetStartCdataSectionHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x458): undefined reference to `XML_SetEndCdataSectionHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x480): undefined reference to `XML_SetDefaultHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x4a8): undefined reference to `XML_SetDefaultHandlerExpand'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x4d0): undefined reference to `XML_SetNotStandaloneHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x4f8): undefined reference to `XML_SetExternalEntityRefHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x520): undefined reference to `XML_SetStartDoctypeDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x548): undefined reference to `XML_SetEndDoctypeDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x570): undefined reference to `XML_SetEntityDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x598): undefined reference to `XML_SetXmlDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x5c0): undefined reference to `XML_SetElementDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x5e8): undefined reference to `XML_SetAttlistDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x610): undefined reference to `XML_SetSkippedEntityHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x6c): undefined reference to `XML_ErrorString'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x9c): undefined reference to `XML_ErrorString'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0xbc): undefined reference to `XML_ErrorString'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x11b): undefined reference to `XML_ErrorString'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x153): undefined reference to `XML_ErrorString'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.text.unlikely+0x173): more undefined references to `XML_ErrorString' follow
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1c62): undefined reference to `XML_GetErrorCode'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1c6d): undefined reference to `XML_GetCurrentColumnNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1c78): undefined reference to `XML_GetCurrentLineNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1c83): undefined reference to `XML_Parse'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1c8e): undefined reference to `XML_ParserCreate_MM'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1c99): undefined reference to `XML_ParserFree'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1ca4): undefined reference to `XML_SetCharacterDataHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1caf): undefined reference to `XML_SetCommentHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1cba): undefined reference to `XML_SetDefaultHandlerExpand'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1cc5): undefined reference to `XML_SetElementHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1cd0): undefined reference to `XML_SetNamespaceDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1cdb): undefined reference to `XML_SetProcessingInstructionHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1ce6): undefined reference to `XML_SetUnknownEncodingHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1cf1): undefined reference to `XML_SetUserData'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1cfc): undefined reference to `XML_SetStartDoctypeDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `PyInit_pyexpat.cold.17':
(.text.unlikely+0x1d07): undefined reference to `XML_SetEncoding'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `xmlparse_getattro.cold.20':
(.text.unlikely+0x267b): undefined reference to `XML_GetCurrentLineNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `xmlparse_getattro.cold.20':
(.text.unlikely+0x26d5): undefined reference to `XML_GetCurrentColumnNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `xmlparse_getattro.cold.20':
(.text.unlikely+0x272a): undefined reference to `XML_GetCurrentByteIndex'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `xmlparse_getattro.cold.20':
(.text.unlikely+0x27ee): undefined reference to `XML_GetCurrentColumnNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `xmlparse_getattro.cold.20':
(.text.unlikely+0x281c): undefined reference to `XML_GetCurrentByteIndex'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `xmlparse_getattro.cold.20':
(.text.unlikely+0x2853): undefined reference to `XML_GetCurrentLineNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `xmlparse_getattro.cold.20':
(.text.unlikely+0x286d): undefined reference to `XML_GetErrorCode'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `pyexpat_xmlparser_Parse.cold.21':
(.text.unlikely+0x28f0): undefined reference to `XML_Parse'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `call_with_frame.isra.7.cold.24':
(.text.unlikely+0x2a9a): undefined reference to `XML_StopParser'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `get_parse_result.cold.27':
(.text.unlikely+0x2b0a): undefined reference to `XML_GetErrorCode'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `call_character_handler.cold.28':
(.text.unlikely+0x2b33): undefined reference to `XML_SetCharacterDataHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `call_character_handler.cold.28':
(.text.unlikely+0x2b95): undefined reference to `XML_SetCharacterDataHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `set_error.isra.8':
(.text.unlikely+0x2e1a): undefined reference to `XML_GetCurrentLineNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `set_error.isra.8':
(.text.unlikely+0x2e25): undefined reference to `XML_GetCurrentColumnNumber'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `set_error.isra.8':
(.text.unlikely+0x2e30): undefined reference to `XML_ErrorString'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `pyexpat_xmlparser_UseForeignDTD':
(.text.unlikely+0x307f): undefined reference to `XML_UseForeignDTD'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `pyexpat_ErrorString':
(.text.unlikely+0x3b4d): undefined reference to `XML_ErrorString'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `pyexpat_xmlparser_GetBase':
(.text.unlikely+0x414e): undefined reference to `XML_GetBase'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `pyexpat_xmlparser_GetInputContext':
(.text.unlikely+0x449b): undefined reference to `XML_GetInputContext'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `flag_error':
(.text.unlikely+0x31e5): undefined reference to `XML_SetExternalEntityRefHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o): In function `my_ElementDeclHandler':
(.text.unlikely+0x3836): undefined reference to `XML_FreeContentModel'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(posixmodule.o): In function `os_openpty':
(.text.unlikely+0x1447): undefined reference to `openpty'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(posixmodule.o): In function `os_forkpty':
(.text.unlikely+0x1e6e): undefined reference to `forkpty'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(faulthandler.o): In function `faulthandler_thread':
(.text.unlikely+0x618): undefined reference to `pthread_sigmask'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__register_frame_info.part.4':
(.text+0x1662): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__register_frame_info_bases':
(.text+0x16e7): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__register_frame_info_table_bases':
(.text+0x17bb): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__deregister_frame_info_bases':
(.text+0x185e): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__deregister_frame_info_bases':
(.text+0x18e6): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `_Unwind_Find_FDE':
(.text+0x19c6): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `_Unwind_Find_FDE':
(.text+0x1a16): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `_Unwind_Find_FDE':
(.text+0x1b00): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__register_frame_info.part.4':
(.text+0x1681): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__register_frame_info_bases':
(.text+0x1706): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `__register_frame_info_table_bases':
(.text+0x17da): undefined reference to `pthread_mutex_unlock'
collect2: error: ld returned 1 exit status

编辑 2: 确切的gcc命令行:

gcc test.c -otest -static $(pkg-config --libs --cflags python3) -lc -lpthread -lexpat -lz -ldl -lutil -lm
编辑3: 这是locate的结果:
$ locate libexpat.a libc.a libm.a libz.a libpthread.a libdl.a libutil.a libpython3.5m.a | grep x86_64-
linux-gnu
/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5m.a
/usr/lib/x86_64-linux-gnu/libc.a
/usr/lib/x86_64-linux-gnu/libdl.a
/usr/lib/x86_64-linux-gnu/libexpat.a
/usr/lib/x86_64-linux-gnu/libm.a
/usr/lib/x86_64-linux-gnu/libpthread.a
/usr/lib/x86_64-linux-gnu/libpython3.5m.a
/usr/lib/x86_64-linux-gnu/libutil.a
/usr/lib/x86_64-linux-gnu/libz.a

所以我应该拥有所有需要的静态库。

这是一个新错误,比第一个错误要短一些:new,并且包含一些简写

$ gcc pregex.c -otest -static $(pkg-config --libs --cflags python3) -lc -lpthread -lexpat -lz -ldl -lutil -lm
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(posixmodule.o): In function `posix_getgrouplist':
(.text.unlikely+0x3aa6): warning: Using 'getgrouplist' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(posixmodule.o): In function `posix_initgroups':
(.text.unlikely+0x3cc1): warning: Using 'initgroups' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(grpmodule.o): In function `grp_getgrall':
(.text+0x2f5): warning: Using 'getgrent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(grpmodule.o): In function `grp_getgrgid':
(.text+0xe9): warning: Using 'getgrgid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(grpmodule.o): In function `grp_getgrnam':
(.text+0x266): warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(grpmodule.o): In function `grp_getgrall':
(.text+0x2c9): warning: Using 'setgrent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(grpmodule.o): In function `grp_getgrall.cold.7':
(.text.unlikely+0x15a): warning: Using 'endgrent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pwdmodule.o): In function `pwd_getpwall':
(.text+0x285): warning: Using 'getpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pwdmodule.o): In function `pwd_getpwnam':
(.text+0x1ee): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pwdmodule.o): In function `pwd_getpwuid':
(.text+0x9f): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pwdmodule.o): In function `pwd_getpwall':
(.text+0x259): warning: Using 'setpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pwdmodule.o): In function `pwd_getpwall.cold.6':
(.text.unlikely+0x14d): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(socketmodule.o): In function `socket_getaddrinfo':
(.text+0x167b): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(spwdmodule.o): In function `spwd_getspall':
(.text.unlikely+0x1a6): warning: Using 'getspent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(spwdmodule.o): In function `spwd_getspnam':
(.text.unlikely+0x27b): warning: Using 'getspnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(spwdmodule.o): In function `spwd_getspall':
(.text.unlikely+0x1a1): warning: Using 'setspent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(spwdmodule.o): In function `spwd_getspall':
(.text.unlikely+0x1f7): warning: Using 'endspent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(socketmodule.o): In function `socket_gethostbyaddr':
(.text+0x2d45): warning: Using 'gethostbyaddr_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(socketmodule.o): In function `socket_gethostbyname_ex':
(.text.unlikely+0x1ae4): warning: Using 'gethostbyname_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(socketmodule.o): In function `socket_getprotobyname':
(.text.unlikely+0x33ff): warning: Using 'getprotobyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(socketmodule.o): In function `socket_getservbyname':
(.text.unlikely+0x2263): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(socketmodule.o): In function `socket_getservbyport':
(.text.unlikely+0x34c3): warning: Using 'getservbyport' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpthread.a(lowlevellock.o): In function `__lll_lock_wait_private':
/build/glibc-Cl5G7W/glibc-2.23/nptl/../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:78: multiple definition of `__lll_lock_wait_private'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libc.a(libc-lowlevellock.o):(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpthread.a(lowlevellock.o): In function `__lll_unlock_wake_private':
/build/glibc-Cl5G7W/glibc-2.23/nptl/../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:328: multiple definition of `__lll_unlock_wake_private'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libc.a(libc-lowlevellock.o):(.text+0x30): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(dynload_shlib.o): In function `_PyImport_FindSharedFuncptr':
(.text+0x7c): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status

编辑4:

现在我可以使用以下命令行编译而不出错:

$ gcc -static test.c -otest $(pkg-config --libs --cflags python3) -lm -lutil -ldl -lz -lexpat -lpthread -lc

我不知道为什么,但是在末尾加上-lc编译就可以了! 问题是输出信息中仍然有警告:

有很多这样的警告:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(dynload_shlib.o): In function `_PyImport_FindSharedFuncptr':
(.text+0x7c): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

2
“error: ld returned 1 exit status” 不是完整的错误信息。 - tkausl
你说得对,我编辑了问题。 - GinoC
找出这些符号来自哪个库,并将其链接进来? - Lightness Races in Orbit
@LightnessRacesinOrbit,好的,我怎么才能找到是哪个库? - GinoC
@GinoC:文档,谷歌,使用ldd命令查看共享库版本。 - Lightness Races in Orbit
@LightnessRacesinOrbit 是的,我遵循了 @MikeKinghan 的答案(使用 ldd 查找依赖项,但仍然无法工作)。 - GinoC
1个回答

12

正如您所看到的,在您失败的链接中所有未定义引用都位于libpython3.5m.a中的链接对象文件中,这是您在链接命令行中通过pkg-config --libs --cflags python3请求的python3库的静态版本。

当链接包括libpython3.5m.a时,要链接一个完全静态可执行文件(-static),链接器还必须找到所有libpython3.5m.a依赖项的静态(*.a)版本1。所有这些依赖项的动态(*.so)版本已安装在您的系统上。这就是为什么:

gcc test.c -otest $(pkg-config --libs --cflags python3)

如果没有使用-static选项,程序可以成功运行。这些依赖的静态版本并未全部安装在您的系统上。因此,当您添加-static选项时会出现所有未定义引用链接错误。

我的系统上安装了python3.6,我看到:

$ pkg-config --libs python-3.6
-lpython3.6m

并且:

$ locate libpython3.6m.so
/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.so
/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1
/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0

libpython3.6m.so 的动态依赖关系如下:

$ ldd /usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.so
    linux-vdso.so.1 =>  (0x00007ffc475af000)
    libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fa87cf6e000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fa87cd51000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa87cb32000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa87c92e000)
    libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fa87c72a000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa87c3d4000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa87bff4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fa87d85a000)

我们可以忽略第一个和最后一个,它们看起来不像常规库,实际上也不是2。因此,我得出结论为了满足libpython3.6a的静态依赖关系,我需要安装以下库的静态版本:

libexpat
libz
libpthread
libdl
libutil
libm
libc

这些库的dev包将提供它们。因为我的系统是64位Ubuntu,所以我会通过以下方式过滤这些dev包:

$ dpkg --search libexpat.a libz.a libpthread.a libdl.a libutil.a libm.a libc.a | grep amd64
libexpat1-dev:amd64: /usr/lib/x86_64-linux-gnu/libexpat.a
zlib1g-dev:amd64: /usr/lib/x86_64-linux-gnu/libz.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libpthread.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libdl.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libutil.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libm.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libc.a

使用以下命令安装它们:

sudo apt install libexpat1-dev zlib1g-dev libc6-dev

虽然你没有透露你的系统是什么,但毫无疑问,你可以使用这种思路来发现libpython3.5m.a中的静态依赖项,无论你的系统是什么。


[1] 严格地说,所有成员依赖的库 libpython3.5m.a (你正在链接的),但我们不会那么苛刻。

[2] 第一个是该库的vDSO,实际上并不是真正的文件。第二个是Linux加载程序。


谢谢您的回复。我使用的是64位Ubuntu。我已经安装了libexpat1-dev zlib1g-dev libc6-dev。所以我尝试将它们链接起来:gcc pregex.c -opregex $(pkg-config --libs --cflags python3) -lc -lpthread -lexpat -lz -ldl -lutil -lm,这个方法可行,但加上-static后与之前一样。 - GinoC
@GinoC 请在您的问题正文中发布您正在使用的带有“-static”选项的确切gcc命令行。 - Mike Kinghan
2
你不需要在 pkg-config 命令中加上 --static 吗? - Lightness Races in Orbit
1
@LightnessRacesinOrbit 我不知道它有那个选项 - 如果它有效,那就比 ldd 推导更好了。添加它会产生依赖项 -lpthread -ldl -lutil -lpython3.5m,而不是仅有的 -lpython3.5m。 如果你看到一个基于这个答案的解决方案,请继续提问。 - Mike Kinghan
我尝试定位所有库的静态版本。我已经编辑了我的问题并附上了结果。 - GinoC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接