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/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 && :
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)
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}")

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?

Thanks.

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


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}
                     $<TARGET_FILE_DIR:bmo>
                     )
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}
                     $<TARGET_FILE_DIR:bmo>
                     )
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) 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

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.