Arm Mbed and Pelion Device Management support forum

Static build failing

I am trying to build the ssl_server.c as static by using the below command.But its not getting build successfully, however it is building properly if i remove the static flag.
Note: i have installed the Mbedtls library at /opt/mbedtls.

LD_LIBRARY_PATH=/opt/mbedtls/lib gcc -static ssl_server.c -I/opt/mbedtls/include/ -L/opt/mbedtls/lib -lmbedcrypto -lmbedx509 -lmbedtls

TLS_SERVER$ LD_LIBRARY_PATH=/opt/mbedtls/lib gcc -static ssl_server.c -o ssl_server -I/opt/mbedtls/include/ -L/opt/mbedtls/lib -lmbedcrypto -lmbedx509 -lmbedtls
/opt/mbedtls/lib/libmbedtls.a(net_sockets.c.o): In function `mbedtls_net_connect':
net_sockets.c:(.text+0xbe): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/opt/mbedtls/lib/libmbedtls.a(ssl_tls.c.o): In function `mbedtls_ssl_psk_derive_premaster':
ssl_tls.c:(.text+0x2004): undefined reference to `mbedtls_ecdh_calc_secret'
ssl_tls.c:(.text+0x2192): undefined reference to `mbedtls_dhm_calc_secret'
/opt/mbedtls/lib/libmbedtls.a(ssl_tls.c.o): In function `mbedtls_ssl_handshake_free':
ssl_tls.c:(.text+0x6379): undefined reference to `mbedtls_dhm_free'
ssl_tls.c:(.text+0x6385): undefined reference to `mbedtls_ecdh_free'
/opt/mbedtls/lib/libmbedtls.a(ssl_tls.c.o): In function `ssl_handshake_init':
ssl_tls.c:(.text+0x6c5d): undefined reference to `mbedtls_dhm_init'
ssl_tls.c:(.text+0x6c69): undefined reference to `mbedtls_ecdh_init'
/opt/mbedtls/lib/libmbedtls.a(ssl_cli.c.o): In function `mbedtls_ssl_handshake_client_step':
ssl_cli.c:(.text+0x260d): undefined reference to `mbedtls_ecdh_make_public'
ssl_cli.c:(.text+0x2917): undefined reference to `mbedtls_ecdh_make_public'
ssl_cli.c:(.text+0x2973): undefined reference to `mbedtls_ecdh_calc_secret'
ssl_cli.c:(.text+0x2b5b): undefined reference to `mbedtls_ecdh_get_params'
ssl_cli.c:(.text+0x3199): undefined reference to `mbedtls_ecdh_read_params'
ssl_cli.c:(.text+0x3322): undefined reference to `mbedtls_dhm_make_public'
ssl_cli.c:(.text+0x33a0): undefined reference to `mbedtls_dhm_calc_secret'
ssl_cli.c:(.text+0x401c): undefined reference to `mbedtls_dhm_make_public'
ssl_cli.c:(.text+0x4085): undefined reference to `mbedtls_dhm_read_params'
/opt/mbedtls/lib/libmbedtls.a(ssl_srv.c.o): In function `ssl_parse_client_dh_public':
ssl_srv.c:(.text+0x21d): undefined reference to `mbedtls_dhm_read_public'
/opt/mbedtls/lib/libmbedtls.a(ssl_srv.c.o): In function `mbedtls_ssl_handshake_server_step':
ssl_srv.c:(.text+0x38be): undefined reference to `mbedtls_ecdh_get_params'
ssl_srv.c:(.text+0x3952): undefined reference to `mbedtls_ecdh_read_public'
ssl_srv.c:(.text+0x39be): undefined reference to `mbedtls_ecdh_calc_secret'
ssl_srv.c:(.text+0x3b0c): undefined reference to `mbedtls_dhm_set_group'
ssl_srv.c:(.text+0x3e53): undefined reference to `mbedtls_dhm_calc_secret'
ssl_srv.c:(.text+0x4387): undefined reference to `mbedtls_ecdh_setup'
ssl_srv.c:(.text+0x4440): undefined reference to `mbedtls_dhm_make_params'
ssl_srv.c:(.text+0x4680): undefined reference to `mbedtls_ecdh_read_public'
ssl_srv.c:(.text+0x4716): undefined reference to `mbedtls_ecdh_make_params'
collect2: error: ld returned 1 exit status

Could you please tell me where am going wrong or what might be the reason behind unsuccessful build.
Any help would be appreciated.
Thanks in advance.

Hi @lijintv07
Thank you for your question!

however it is building properly if i remove the static flag.

This probably means you have proper configuration

Could you confirm that these symbols are defined in libmbedcrypto ?

nm libmbedcrypto

In static compilation, the order of the libraries given as input for linker matter, according to library dependency, so I would guess that this is probably the issue.

The simplest solution fore your problem might be to do the following:

LD_LIBRARY_PATH=/opt/mbedtls/lib gcc -static ssl_server.c -I/opt/mbedtls/include/ -L/opt/mbedtls/lib -lmbedcrypto -lmbedx509 -lmbedtls -lmbedcrypto -lmbedx509 -lmbedtls

(twice the library list).

Please update if that helped.
Regards,
Mbed TLS Support
Ron

Thank you @roneld01 , its working after applying your said method.