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

Hi there,

Recently, I wanted to use C++ to write some torch code using CLion and it gave me an error like this:

: && /Applications/ -g -arch arm64 -isysroot /Applications/ -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/  /Users//Desktop/LearnCpp/hello/libtorch/lib/  /Users//Desktop/LearnCpp/hello/libtorch/lib/libkineto.a  -Wl,--no-as-needed,"/Users//Desktop/LearnCpp/hello/libtorch/lib/" -Wl,--as-needed  /Users//Desktop/LearnCpp/hello/libtorch/lib/  -lmkl_intel_ilp64  -lmkl_core  -lmkl_intel_thread  -Wl,--no-as-needed,"/Users//Desktop/LearnCpp/hello/libtorch/lib/" -Wl,--as-needed && :
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.

And my CMakeLists.txt is:

cmake_minimum_required(VERSION 3.28)


add_executable(hello main.cpp)

set(CMAKE_PREFIX_PATH libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
target_link_libraries(hello "${TORCH_LIBRARIES}")

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?


I didn’t use CLion, but I got this cmake to work in vscode:

cmake_minimum_required(VERSION 3.18 FATAL_ERROR)

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

add_executable(bmo bmo.cpp ${BMO_SRC})

# Torch
find_package(Torch REQUIRED)

target_link_libraries(bmo "${TORCH_LIBRARIES}")

# The following code block is suggested to be used on Windows.
# According to,
# 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
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
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
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
endif (APPLE)

These are the notes I took while setting it up:

Overview (CPU torch)
- download libtorch and extract into the root folder (you should have a 'libtorch' folder now in the root of the project)
- 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: (with the shenanigans above)
- AppleClang

And here’s the commands I used to make the project

rm -r build
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug

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

Thank you, Brandon! I will try it out!

Can you share me the whole example code you did on Apple Silicon please?

I got a linker error.