Compiling LibTorch project in CLion on Apple M1

I am able to successfully compile and run this example-app using Rosetta 2 in a Terminal. However, when I try to do the same thing in CLion, I get the following linking error
ld: warning: ignoring file ../libtorch/lib/libc10.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file ../libtorch/lib/libkineto.a, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file ../libtorch/lib/libc10.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file ../libtorch/lib/libtorch.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file ../libtorch/lib/libtorch_cpu.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
Undefined symbols for architecture arm64:
  "at::rand(c10::ArrayRef<long long>, c10::TensorOptions)", referenced from:
      torch::rand(c10::ArrayRef<long long>, c10::TensorOptions) in example-app.cpp.o
  "at::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, at::Tensor const&, long long)", referenced from:
      at::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, at::Tensor const&) in example-app.cpp.o
  "c10::TensorImpl::set_autograd_meta(std::__1::unique_ptr<c10::AutogradMetaInterface, std::__1::default_delete<c10::AutogradMetaInterface> >)", referenced from:
      torch::autograd::make_variable(at::Tensor, bool, bool) in example-app.cpp.o
  "c10::UndefinedTensorImpl::_singleton", referenced from:
      c10::UndefinedTensorImpl::singleton() in example-app.cpp.o
  "c10::AutogradMetaInterface::~AutogradMetaInterface()", referenced from:
      torch::autograd::AutogradMeta::AutogradMeta(c10::TensorImpl*, bool, torch::autograd::Edge) in example-app.cpp.o
  "c10::impl::ExcludeDispatchKeyGuard::ExcludeDispatchKeyGuard(c10::DispatchKeySet)", referenced from:
      at::AutoDispatchBelowADInplaceOrView::AutoDispatchBelowADInplaceOrView() in example-app.cpp.o
  "c10::impl::ExcludeDispatchKeyGuard::~ExcludeDispatchKeyGuard()", referenced from:
      at::AutoDispatchBelowADInplaceOrView::~AutoDispatchBelowADInplaceOrView() in example-app.cpp.o
  "c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*)", referenced from:
      torch::autograd::AutogradMeta::AutogradMeta(c10::TensorImpl*, bool, torch::autograd::Edge) in example-app.cpp.o
      c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, c10::detail::CompileTimeEmptyString) in example-app.cpp.o
  "c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      c10::Device::validate() in example-app.cpp.o
  "c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, char const*)", referenced from:
      c10::intrusive_ptr<c10::VariableVersion::VersionCounter, c10::detail::intrusive_target_default_null_type<c10::VariableVersion::VersionCounter> >::intrusive_ptr(c10::VariableVersion::VersionCounter*) in example-app.cpp.o
      c10::intrusive_ptr_target::~intrusive_ptr_target() in example-app.cpp.o
      c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::retain_() in example-app.cpp.o
  "vtable for c10::AutogradMetaInterface", referenced from:
      c10::AutogradMetaInterface::AutogradMetaInterface() in example-app.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for torch::autograd::AutogradMeta", referenced from:
      torch::autograd::AutogradMeta::AutogradMeta(c10::TensorImpl*, bool, torch::autograd::Edge) in example-app.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [example-app] Error 1
make[2]: *** [CMakeFiles/example-app.dir/all] Error 2
make[1]: *** [CMakeFiles/example-app.dir/rule] Error 2
make: *** [example-app] Error 2

This only happens when I attempt to compile the project from scratch in CLion. If I first compile the project in a Terminal, I am able to successfully compile and run modifications to the example using the CLion interface. Furthermore, I am not able to debug the example at all.

I have set the CMake options in CLion to use -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..

Please sign in to leave a comment.