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

2024-01-04

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

cython3 -3 test.py --embed

Then

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

编译 C 生成的文件。

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

我能怎么做?

EDIT 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

EDIT 2:确切的 gcc 命令行:

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

EDIT 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错误,有点shorter比第一个:

$ 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

EDIT 4:

好的,现在我可以compile此命令行没有错误:

$ 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

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

To link a fully static executable (-static) when the linkage includes libpython3.5m.a, the linker must also find static (*.a) versions of all the libraries that libpython3.5m.a depends upon1. The dynamic (*.so) versions of all those dependencies are installed on your system. That is why:

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

成功,无需-static。这些依赖项的静态版本是 并非全部安装在您的系统上。因此所有未定义的参考 添加时出现链接错误-static.

我自己的系统有python3.6,我看到:

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

and:

$ 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 are:

$ 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)

We can disregard the first and last ones, which don't look like regular libraries and indeed aren't2. So, I'd conclude that to satisfy the static dependencies of libpython3.6a, I need to install the static versions of:-

libexpat
libz
libpthread
libdl
libutil
libm
libc

这将由这些库的开发包提供。由于我的系统是 64 位 Ubuntu,因此我会过滤这些开发包 经过:

$ 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] Strictly, all libraries that the members of libpython3.5m.a that you are linking depend upon, but we won't be that pernickity.

[2] 第一个是图书馆的vDSO https://en.wikipedia.org/wiki/VDSO, 根本不是真正的文件。第二个是linux加载器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cython:编译独立的静态可执行文件 的相关文章

随机推荐

  • Python-将字符串中的单词与字符串列表进行匹配

    我是 python 新手 我想知道字符串比较是如何完成的 假设我有一个包含州名称的字符串列表 例如 states New York California Nebraska Idaho 我还有另一个包含地址的字符串 例如 postal add
  • 从 Django 模型获取数据并使用 AJAX 在 html 表中显示

    我已经搜索并实现了很多不同的方法来显示 html 表 使用 AJAX 从 JsonResponse Django 但无济于事 目前 我得到的最远的是对网络控制台的响应 products model products product pk 2
  • 多列上的 SELECT COUNT(DISTINCT...) 错误?

    我有一个表 VehicleModelYear 其中包含列 id 年份 品牌和型号 以下两个查询按预期工作 SELECT DISTINCT make model FROM VehicleModelYear SELECT COUNT DISTI
  • JS Regex - 替换 Markdown 链接的内容

    我几乎已经解决了一个正则表达式问题 只是一件小事 我正在尝试得到这个 and so use chalk api red string options 进入这个 and so use chalk red string options 我有这个
  • matplotlib 图例中的两个相邻符号

    我想在图例中的同一行上识别两个不同的符号 具有不同的颜色 下面 我尝试使用代理艺术家执行此操作 但结果是它们在图例中彼此堆叠 我希望它们彼此相邻或一个在另一个之上 这样它们都是可见的 from pylab import import mat
  • C# - XML - 压缩

    我遇到过这样的情况 我正在生成要提交到 Web 服务的 XML 文件 有时由于数据量超过 30mb 或 50mb 我需要使用 c net Framework 4 0 来压缩文件 而不是使用拥有大部分数据的节点之一 我不知道我要怎么做 如果有
  • Tomcat HTTP Access 日志写入文件有延迟

    在 tomcat 中 http 访问日志 Valve 需要一些时间才能写入文件 请注意 我有 org apache catalina valves AccessLogValve 的默认配置 有什么办法可以改善延迟吗 造成这种延迟的主要原因是
  • 在 Windows 上通过 Jenkins 使用 chromedriver 和 chrome 捕获屏幕截图时,从渲染器接收消息超时:10.000

    操作系统 Windows 10 浏览器 Chrome 浏览器版本 版本73 0 3683 86 官方版本 32位 我正在运行 selenium cucumber BDD 项目 我正在验证一页的标题 我正在使用范围报告版本4 项目在本地运行成
  • afterAjaxUpdate 回调函数 CListView 显示未定义

    我试图调用一个在另一个 js 文件中通过文件名的 afterAjaxUpdate 参数定义的函数 但我在控制台中收到错误 该函数未定义
  • 在同一行显示图标和文本

    我试图让文本 主页 关于我们 奶酪 等显示在社交媒体图标的右侧 以便文本的底部与图标的底部对齐 并且它们显示在同一行 我该怎么做呢 我的代码在这里摆弄http jsfiddle net pnX3d http jsfiddle net pnX
  • /etc/rc.local 和 ~/.bashrc 有什么区别?

    这是一个与linux相关的问题 我四处搜寻但没有得到很好的解释 在我看来 这两个文件在我登录时都会配置设置 但是有什么区别吗 我注意到在决定哪些内容应该放入两个不同的文件中似乎有 一些规则 例如 如果我需要向 PATH 添加特定的搜索路径
  • 如何使用apache poi 3.6获取A1(单元格地址)的单元格值

    我有 Excel 单元格地址 如 A1 A2 那么 如何使用 poi 3 6 以编程方式访问此单元格 另一种方法是 row mySheet getRow cell row getCell 但我的地址格式为 A1 那么 我如何以编程方式访问这
  • 如何使用模板函数从缓冲区(T* 数据数组)创建 cv::Mat?

    我想编写一个模板函数来复制指针引用的数据T image to cv Mat 我很困惑如何概括 T 和 cv type 匹配 template
  • 将 Android studio 更新到 2.2 后出现“无法解决 x”错误

    Google 推出了 Android Studio 2 2 所以我将 Android studio 2 1 更新为 Android Studio 2 2 当我制作项目时 我收到这些错误 和更多 Error Failed to resolve
  • 添加apache用户组文件上传权限

    我有一个 php 脚本将文件上传到某个文件夹 目前它们作为 psacln 组上传 以便我可以通过 FTP 删除文件 当 PHP 作为 FastCGI 运行时 这一切都工作正常 我必须将 PHP 更改为作为 Apache 模块运行 以便让 p
  • 为什么Ul上方有空白

    当我放置无序列表时 then why top bar goes down I cannot find the reason behind this Please tell me how to fix this problem http js
  • Joomla MySQL 性能

    我一直在开发一个版本为2 5 11的Joomla网站 网站的流量将会非常高 我的问题是关于 MySQL 查询性能的 数据库包含大约 60000 行内容表 下面看到的查询 核心 com content 文章模型查询 执行时间约为 6 秒 非常
  • 如何显示尾随“%”符号?

    在 SO Line 的折扣百分比列中 我想在其中显示尾随 我尝试将显示格式设置为 P 但这是一场灾难 由于实际值是数据库中的百分比 因此不必乘以 100 感谢您的回答 您应该设置DisplayFormat to 0 00 结果你会得到以下结
  • Py2Exe 检测为病毒。备择方案?

    所以 我创建了一个python程序 使用 Py2Exe 转换为 exe 并尝试使用 PyInstaller 和 cx freeze 所有这些都会触发程序在virustotal 和我的本地计算机上被avast avg 和其他程序检测为病毒 我
  • Cython:编译独立的静态可执行文件

    我正在尝试编译一个不使用动态加载器的可执行文件 ELF 文件 我用了Cython将Python编译为C cython3 3 test py embed Then gcc test c otest pkg config libs cflags