0

I am trying to get the intel math kernel library (mkl) to run. There is a tool (https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor) that prints the necessary, environment-dependent cmd commands to use it for C++ scripting. For me it says:

Use this link line: ${MKLROOT}/lib/libmkl_intel_lp64.a ${MKLROOT}/lib/libmkl_intel_thread.a ${MKLROOT}/lib/libmkl_core.a -liomp5 -lpthread -lm -ldl

Compiler options: -m64 -I${MKLROOT}/include

My goal is to write this in a CMake script. Where am I going wrong / what do I have to write to make this work?

cmake_minimum_required(VERSION 3.15)
project(PSD_Projections)

set(CMAKE_CXX_STANDARD 14)
set(MKL_DIR /opt/intel/mkl)

# Part for the linker line
# This seems to be somewhat okay
# However I can't figure out what to do about the other linker line arguments
find_library(LIB1 mkl_intel_lp64 ${MKL_DIR}/lib)
find_library(LIB2 mkl_intel_thread ${MKL_DIR}/lib)
find_library(LIB3 mkl_core ${MKL_DIR}/lib)
link_libraries(${LIB1} ${LIB2} ${LIB3})

# Part for the compiler options.
# ${MKL_DIR}/include is found and exists
# I don't know what to do about the -m64 
include_directories(${MKL_DIR}/include)

add_executable(PSD_Projections main.cpp)
schajan
  • 59
  • 5

1 Answers1

2

An example for sequential ILP64 version:

target_include_directories(PSD_Projections PUBLIC "${MKL_DIR}/include")
target_compile_definitions(PSD_Projections PUBLIC MKL_ILP64)
target_link_directories(PSD_Projections PUBLIC "${MKL_DIR}/lib/intel64")
target_link_libraries(PSD_Projections PUBLIC mkl_intel_ilp64 mkl_sequential mkl_core m dl)
target_link_options(PSD_Projections PUBLIC "-Wl,--no-as-needed")

It can easily be adjusted to your needs.

An example how to set C++ standard version and compiler options:

target_compile_features(PSD_Projections PUBLIC cxx_std_14)
target_compile_options(PSD_Projections PUBLIC -Wall -Wpedantic -Wextra -m64 -march=native)
Evg
  • 25,259
  • 5
  • 41
  • 83
  • 1
    I agree, you should be using `target_*` cmake functions to localize the scope of your actions. The CMake code in your example is very old-style. For example the `include_directories` and `link_libraries` functions are listed as deprecated within the CMake documentation itself now. – tjwrona1992 Nov 13 '19 at 18:49
  • `"-Wl,--no-as-needed"` that can be put in `target_link_options` – Guillaume Racicot Nov 13 '19 at 18:53
  • @GuillaumeRacicot, from the point of view of best practices, can be put of should be put? – Evg Nov 13 '19 at 18:55
  • 1
    @Evg should. Just like it's better to call `target_compile_definitions` than passing `-DXYZ` in `target_compile_options` – Guillaume Racicot Nov 13 '19 at 18:59
  • 1
    @Evg searching around, there's a target property [`LINK_WHAT_YOU_USE`](https://cmake.org/cmake/help/latest/prop_tgt/LINK_WHAT_YOU_USE.html) that automatically adds the required flags such as `--no-as-needed` – Guillaume Racicot Nov 13 '19 at 19:11