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.