Can I compile C++ code using libtorch (c++) on Apple Silicon?

yong1990: <p>Hi there,</p>
<p>Recently, I wanted to use C++ to write some torch code using CLion and it gave me an error like this:</p>
<pre><code class="lang-auto">: &amp;&amp; /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/hello.dir/main.cpp.o -o hello -L/Users//miniconda3/lib   -L/Users/y/miniconda3/lib/intel64   -L/Users/yo/miniconda3/lib/intel64_win   -L/Users/yo/miniconda3/lib/win-x64 -Wl,-rpath,/Users/yo/miniconda3/lib -Wl,-rpath,/Users//miniconda3/lib/intel64 -Wl,-rpath,/Users//miniconda3/lib/intel64_win -Wl,-rpath,/Users//miniconda3/lib/win-x64  /Users//Desktop/LearnCpp/hello/libtorch/lib/libtorch.so  /Users//Desktop/LearnCpp/hello/libtorch/lib/libc10.so  /Users//Desktop/LearnCpp/hello/libtorch/lib/libkineto.a  -Wl,--no-as-needed,"/Users//Desktop/LearnCpp/hello/libtorch/lib/libtorch_cpu.so" -Wl,--as-needed  /Users//Desktop/LearnCpp/hello/libtorch/lib/libc10.so  -lmkl_intel_ilp64  -lmkl_core  -lmkl_intel_thread  -Wl,--no-as-needed,"/Users//Desktop/LearnCpp/hello/libtorch/lib/libtorch.so" -Wl,--as-needed &amp;&amp; :
ld: unknown options: --no-as-needed --as-needed --no-as-needed --as-needed
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
</code></pre>
<p>And my CMakeLists.txt is:</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.28)
project(hello)

set(CMAKE_CXX_STANDARD 17)

add_executable(hello main.cpp)

set(CMAKE_PREFIX_PATH libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
target_link_libraries(hello "${TORCH_LIBRARIES}")
</code></pre>
<p>I do not know which part is not configured correctly, but can anyone give me an example how to use c++ libtorch on Mac OS with an Apple Silicon and an examplar CMakeLists.txt for CLion?</p>
<p>Thanks.</p>

Brandon_Fowler: <p>I didn’t use CLion, but I got this cmake to work in vscode:</p>
<pre><code class="lang-auto">
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(bmo)

file(GLOB BMO_SRC "BMO/**/*.cpp")

add_executable(bmo bmo.cpp ${BMO_SRC})

# Torch
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/libtorch")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

target_link_libraries(bmo "${TORCH_LIBRARIES}")
set_property(TARGET bmo PROPERTY CXX_STANDARD 17)

# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
  file(GLOB TORCH_DLLS "${CMAKE_SOURCE_DIR}/libtorch/lib/*.dll")
  message("DLL files: ${TORCH_DLLS}")
  add_custom_command(TARGET bmo
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $&lt;TARGET_FILE_DIR:bmo&gt;
                     )
endif (MSVC)


# LDFLAGS="-L/opt/homebrew/opt/libomp/lib"
set(LDFLAGS "-L/opt/homebrew/opt/libomp/lib")
# The following code block is for macOS.
# It copies the .dylib files to avoid dynamic linking errors.
if (APPLE)
  file(GLOB TORCH_DYLIBS "${CMAKE_SOURCE_DIR}/libtorch/lib/*.dylib")
  message("DYLIB files: ${TORCH_DYLIBS}")
  add_custom_command(TARGET bmo
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DYLIBS}
                     $&lt;TARGET_FILE_DIR:bmo&gt;
                     )
endif (APPLE)
</code></pre>
<p>These are the notes I took while setting it up:</p>
<pre><code class="lang-auto">Overview (CPU torch)
- download libtorch and extract into the root folder (you should have a 'libtorch' folder now in the root of the project) https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-2.3.0.zip
- brew install libomp
- brew info libomp
- navigate to '/opt/homebrew/opt/libomp/lib' or whatever you find in the info cmd
- copy the files to libtorch/lib in this project



Stable versions
- libtorch: https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-2.3.0.zip (with the shenanigans above)
- AppleClang 14.0.3.14030022
</code></pre>
<p>And here’s the commands I used to make the project</p>
<pre><code class="lang-auto">rm -r build
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug
</code></pre>
<p>Then I could use vscode to run that executable and debug my code. It was kind of hard to set up though. Sorry this is kind of crazy path to follow</p>

yong1990: <p>Thank you, Brandon! I will try it out!</p>

yong1990: <aside class="quote no-group" data-username="Brandon_Fowler" data-post="2" data-topic="203030">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brandon_fowler/48/68562_2.png" class="avatar"> Brandon_Fowler:</div>
<blockquote>
<p><code>cmake --build . --config Debug</code></p>
</blockquote>
</aside>
<p>Can you share me the whole example code you did on Apple Silicon please?</p>

yong1990: <p>I got a linker error.</p>