Search code examples
cgcccmakeintel-mkl

Undefined reference to `sqrt` despite linking to math library


I am trying to compile an example program from the Intel MKL library (/opt/intel/mkl/examples/versionqueryc/). I copied the source code (C-file) to a new directory. I then went and tried to build this example using CMake.

In the course of debugging this, I am stuck when trying to use the following command (isolated from cmake by now).

It doesn't find the Math library but I have -lm included in the linking. So what is going on?

:~/devel/mkl/MKL Test/build$  /usr/bin/gcc -m64 CMakeFiles/mkltest.dir/main.c.o \
>     -o mkltest    -rdynamic -L/home/myuser/src/intel/mkl/lib/intel64 \
>     -lm  -lmkl_intel_lp64 -lmkl_sequential -lmkl_core  -lmkl_cdft_core \
>     -lmkl_scalapack_lp64 \
>     /home/myuser/src/intel/lib/intel64/libiomp5.so \
>     -Wl,-rpath,/home/myuser/src/intel/mkl/lib/intel64:/home/myuser/src/intel/lib/intel64 
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `logf'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `atan2'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `sin'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `fabs'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `exp'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `sqrtf'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `cos'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `sqrt'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_sequential.so: undefined reference to `log'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `pow'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `log10'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `ceil'
/home/myuser/src/intel/mkl/lib/intel64/libmkl_core.so: undefined reference to `expf'
collect2: ld returned 1 exit status

Solution

  • Put -lm as the last parameter, the order of parameters is important while linking.