CMake/CLion: Linker error

Hello,
I am just starting out with libtorch and seem to have run into a rather serious linker issue. Previously, I was able to at least build other projects with torch (that others had created), but now that I’ve tried to create my own simple file, nothing seems to be building anymore.

I have manually installed the latest MacOS build in my root directory:
/Users/jlenz/libtorch/share/cmake/Torch

I am using CLion and have the following CmakeLists.txt:

cmake_minimum_required(VERSION 3.23)
project(midi_plugin)

set(CMAKE_CXX_STANDARD 17)
add_executable(midi_plugin main.cpp)

set(CMAKE_PREFIX_PATH "~/libtorch/share/cmake")
find_package(Torch REQUIRED)

target_link_libraries(midi_plugin "${TORCH_LIBRARIES}")

One thing that is noticeable, although the cmake ‘succesfully’ builds, it displays this in the process:

-- MKL_ARCH: intel64
-- MKL_ROOT /Users/jlenz/opt/miniconda3
-- MKL_LINK: dynamic
-- MKL_INTERFACE_FULL: intel_ilp64
-- MKL_THREADING: intel_thread
-- MKL_MPI: mpich

Once I try to build the project (which is just ‘hello world’ with torch included) I get the following error:

FAILED: midi_plugin 
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -mmacosx-version-min=12.5 -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/midi_plugin.dir/main.cpp.o -o midi_plugin  -Wl,-rpath,/Users/jlenz/libtorch/lib  /Users/jlenz/libtorch/lib/libc10.dylib  /Users/jlenz/libtorch/lib/libkineto.a  /Users/jlenz/libtorch/lib/libtorch.dylib  /Users/jlenz/libtorch/lib/libtorch_cpu.dylib  /Users/jlenz/libtorch/lib/libc10.dylib  -lmkl_intel_ilp64  -lmkl_core  -lmkl_intel_thread && :
ld: library not found for -lmkl_intel_ilp64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Would anyone know how to solve this? I have tried addressing it with a colleague who has used libtorch quite a bit and he can’t identify the issue either. Many thanks in advance.

Since you are linking against MKL I assume your workstation uses an x86 CPU?
If so, did you conda install mkl mkl-include (or installed MKL in another way) before starting the build?

Hi there, coming back to comment that thankfully I solved the issue. In case someone else encounters similar errors in the future, I was able to fix it by simply re-installing pytorch with brew:

brew install pytorch

Apparently I had some corrupted files, which it asked me permission to override. After allowing this, I went back and all of my torch projects were working perfectly.

Hi @ptrblck,

Reopening this thread because, it turns out my cmake was referencing a global dir of pytorch this whole time - but in order to compile and distribute my programme I need it to dynamically link the version packaged within. When trying to change this, I then ended up getting the same error again.

I have a conda installation of mkl as above, with no fix. I should also mention that I’m regularly using pytorch in a python environment without issue.

When I try to run my cmake, it ‘sucesfully’ downloads torch and places it in the appropriate subdir, but displays this message:

-- MKL_ARCH: None, set to ` intel64` by default
-- MKL_ROOT /Users/jlenz/opt/miniconda3
-- MKL_LINK: None, set to ` dynamic` by default
-- MKL_INTERFACE_FULL: None, set to ` intel_ilp64` by default
-- MKL_THREADING: None, set to ` intel_thread` by default
-- MKL_MPI: None, set to ` mpich` by default

And then when I try to build the application:

ld: library not found for -lmkl_intel_ilp64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am wondering if you have any thoughts on where to proceed with troubleshooting this?