Linking to Pytorch on Linux (CPU) fails - cannot find MKL

Hi All,

I encounter the following linker errors trying to link my app to libtorch 1.10.1 (Linux, CPU, C++):

libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_zd_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_d_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_dsbrdbn'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_zlaeh2'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_d_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_s_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_zd_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_trans_mkl_zimatcopy_square_t_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_sc_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_claeh2'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_slaeh2'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_d_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_dlaed2'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_c_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_zhbrdbn'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_dz_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_c_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_s_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_s_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_cs_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_trans_mkl_comatcopy2_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_dlaeh2'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_z_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_sc_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_cs_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_z_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_dlaed3'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_z_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_slaed3'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_trans_mkl_cimatcopy_square_t_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_z_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_c_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_ssbrdbn'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_trans_mkl_zomatcopy2_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_dz_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_s_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_forward_d_out_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_dft_compute_backward_c_par'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_chbrdbn'
libtorch/lib/libtorch_cpu.so: undefined reference to `mkl_lapack_slaed2'

I do list every single .so file in libtorch/lib in the g++ command. My app is not designed to be used with cmake, so not using it. Could build just fine against libtrorch 1.8.2, but failing with 1.10.1.

Would be grateful for any suggestions how to troubleshoot this.

With libtorch 1.8.2 the following was sufficient: -D_GLIBCXX_USE_CXX11_ABI=1 -Llibtorch/lib -ltorch -ltorch_cpu -lc10. With 1.10.1 there are these undefined reference errors for MKL, even when linking all other .so libs.

Issue https://github.com/pytorch/pytorch/issues/72653 solved.