Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython: Compile a Standalone Static Executable

I'm trying to compile an executable (ELF file) that does not use a dynamic loader. I used Cython to compile Python to C:

cython3 -3 test.py --embed

Then

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

to compile C generated file.

Now I'd like to compile test.c in a static executable. Usually I use -static flag, but here it return collect2: error: ld returned 1 exit status.

How can I do?

EDIT 1: Not the full messge becouse body is limited to 30000 chars

a long list of warnings and errors like these:

...
/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: The exact gcc command line:

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

EDIT 3: This is the result of 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

So I should have all static libraries that I need.

This is the new error, a bit shorter than the first:

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

Ok, now I'm able to compile without errors with this command line:

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

I don't know why but with -lc at the end it compile! The problem is that warnings are still in the output message:

Lots of warnings like this:

/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
like image 413
GinoC Avatar asked Feb 09 '18 10:02

GinoC


1 Answers

As you can see, all of the undefined references in your failed linkage lie in linked object files that are members of libpython3.5m.a, which is the static version of the python3 library requested in your linkage commandline by 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)

succeeds, without -static. The static versions of those dependencies are not all installed on your system. Hence all the undefined reference linkage errors when you add -static.

My own system has python3.6, and I see:

$ 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

The dynamic dependencies of 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

which will be provided by the dev packages of those libraries. Since my system is 64 bit Ubuntu, I'd then filter those dev packages by:

$ 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

and install them with:

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

You haven't disclosed what your system is, but no doubt you can adapt this thinking to discover the static dependencies of libpython3.5m.a, whatever your system is.


[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] The first one is the library's vDSO, not a real file at all. The second one is the linux loader.

like image 156
Mike Kinghan Avatar answered Oct 25 '22 12:10

Mike Kinghan