2

I'm setting up a new Linux c++ environment and need to use MKL libraries as part of a project. I tried for the whole day to set it up without success and at a loss of what to do next.

I have installed CLion, CMake, Intel MKL libraries from their website. I'm on Ubuntu 18.04. I got a FindMKL.cmake file from https://gist.github.com/scivision/5108cf6ab1515f581a84cd9ad1ef72aa, and wrote the simplest CMakeLists.txt.

cmake_minimum_required(VERSION 3.10)
project(mytest)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})

add_executable(mytest main.cpp)

find_package(MKL REQUIRED)
if(MKL_FOUND)
    include_directories(${MKL_INCLUDE_DIRS})
    target_link_libraries(mytest ${MKL_CORE_LIBRARY})
else()
    message(WARNING "MKL libs not found")
endif()

MKL is successfully found. I added #include "mkl_lapacke.h" to main.cpp and tried to call a simple function for the MKL library.

#include "mkl_lapacke.h"

void test_mkl() {
  auto p = new float;
  float res =  LAPACKE_slange(
    5, 'a', 3, 5, p, 4); //toy example
}

However, when I build, I get the following

ananas@ananas-Ubuntu:~/CLionProjects/test/cmake-build-debug$ cmake --build .
Scanning dependencies of target mytest
[ 50%] Building CXX object CMakeFiles/mytest.dir/main.cpp.o
[100%] Linking CXX executable mytest
CMakeFiles/mytest.dir/main.cpp.o: In function `test_mkl()':
main.cpp:(.text+0x38): undefined reference to `LAPACKE_slange'
collect2: error: ld returned 1 exit status
CMakeFiles/mytest.dir/build.make:95: recipe for target 'mytest' failed
make[3]: *** [mytest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mytest.dir/all' failed
make[2]: *** [CMakeFiles/mytest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/mytest.dir/rule' failed
make[1]: *** [CMakeFiles/mytest.dir/rule] Error 2
Makefile:118: recipe for target 'mytest' failed
make: *** [mytest] Error 2

I'm at a complete loss as to why it's not working (I tried Google to no avail).

RESOLVED: As mentioned in the comments, it was a linking error. The following was needed to properly link

target_link_libraries(mytest "-Wl,--start-group" ${MKL_LIBRARIES} "-Wl,--end-group -lpthread -lm -ldl")

The extra flags "-lpthread -lm -ldl" made it work for some of the functions, and adding --start/end-group was necessary for some other so that the order the libraries are imported works.

TDC
  • 21
  • 1
  • 3
  • You need to actually *link* your code with the library (what you have are *linker* errors). Including the headers satisfy the compiler, now you need to appease the linker. – Jesper Juhl May 18 '19 at 16:47
  • I was under the impression that target_link_libraries(mytest ${MKL_LIBRARIES}) in the CMakeLists.txt was doing that job? – TDC May 18 '19 at 16:51
  • it does, but you still need to also link to the library defining `dlopen` and friends. – Jesper Juhl May 18 '19 at 16:53
  • Thanks for the link above. I am indeed aware of what linker errors are and have had no issues compiling/linking programs in the past (my setup at work is windows with MSVC). The specific issue here is with linking the MKL library. I am actually writing my CMake file exactly as in the suggested example on the GitHub link above. In my main.cpp file, I'm only calling one of the MKL subroutines, and not any of the ones it complains about. I've edited my question to provide more info. I appreciate any help – TDC May 18 '19 at 17:10
  • @TDC: You have the **static** MKL library (you even set `MKL_USE_STATIC_LIBS` variable for obtain static library). Unlike to *dynamic* libraries, *static* ones doesn't store list of other libraries (`dl` library in your case), which are needed to work. You need to link with that libraries **explicitely**. About linking `dl` library in CMake, see that question: https://stackoverflow.com/questions/33678965/need-to-link-cmake-project-to-dl-library. – Tsyvarev May 18 '19 at 17:31

0 Answers0