我正在建立一个新的Linux c ++环境,需要将MKL库用作项目的一部分。我一整天都在尝试设置它,但没有成功,而且又迷失了下一步。
我已经从其网站上安装了CLion,CMake和Intel MKL库。我在Ubuntu 18.04上。我从https://gist.github.com/scivision/5108cf6ab1515f581a84cd9ad1ef72aa得到了一个FindMKL.cmake文件,并编写了最简单的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。我在main.cpp中添加了#include“ mkl_lapacke.h”,并尝试为MKL库调用一个简单的函数。
#include "mkl_lapacke.h"
void test_mkl() {
auto p = new float;
float res = LAPACKE_slange(
5, 'a', 3, 5, p, 4); //toy example
}
但是,当我构建时,得到以下内容
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
我完全不知道为什么它不起作用(我尝试Google无效)。
已解决: 如评论中所述,这是链接错误。需要以下内容才能正确链接
target_link_libraries(mytest "-Wl,--start-group" ${MKL_LIBRARIES} "-Wl,--end-group -lpthread -lm -ldl")
额外的标志“ -lpthread -lm -ldl”使其对某些功能有效,对于其他功能,必须添加--start / end-group以便导入库。