CMake/CLion: Linker error

username: JLenz
cooked: <p>Hello,<br>
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.</p>
<p>I have manually installed the latest MacOS build in my root directory:<br>
/Users/jlenz/libtorch/share/cmake/Torch</p>
<p>I am using CLion and have the following CmakeLists.txt:</p>
<pre><code class="lang-auto">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}")
</code></pre>
<p>One thing that is noticeable, although the cmake ‘succesfully’ builds, it displays this in the process:</p>
<pre><code class="lang-auto">-- MKL_ARCH: intel64
-- MKL_ROOT /Users/jlenz/opt/miniconda3
-- MKL_LINK: dynamic
-- MKL_INTERFACE_FULL: intel_ilp64
-- MKL_THREADING: intel_thread
-- MKL_MPI: mpich
</code></pre>
<p>Once I try to build the project (which is just ‘hello world’ with torch included) I get the following error:</p>
<pre><code class="lang-auto">FAILED: midi_plugin 
: &amp;&amp; /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 &amp;&amp; :
ld: library not found for -lmkl_intel_ilp64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
</code></pre>
<p>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.</p>

username: ptrblck
cooked: <p>Since you are linking against MKL I assume your workstation uses an x86 CPU?<br>
If so, did you <code>conda install mkl mkl-include</code> (or installed MKL in another way) before starting the build?</p>

username: JLenz
cooked: <p>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:</p>
<pre><code class="lang-auto">brew install pytorch
</code></pre>
<p>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.</p>

username: JLenz
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>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.</p>
<p>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.</p>
<p>When I try to run my cmake, it ‘sucesfully’ downloads torch and places it in the appropriate subdir, but displays this message:</p>
<pre><code class="lang-auto">-- 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
</code></pre>
<p>And then when I try to build the application:</p>
<pre><code class="lang-auto">ld: library not found for -lmkl_intel_ilp64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
</code></pre>
<p>I am wondering if you have any thoughts on where to proceed with troubleshooting this?</p>