Get errors after compiling and running PyTorch MINIMAL EXAMPLE for c++ Mac M1 with make
Labels: module: build, module: cpp, triaged

### 🐛 Describe the bug

I am able to download and compile the MINIMAL Example for C++ on Mac with Cmake. I run the cmake command and everything finished with no errors. However, when I go and execute the "make" command I get errors related to "Undefined symbols for architecture arm64" for c10.


### **_This command works_**
❯ cmake -DCMAKE_PREFIX_PATH=/Users/raulcardona/pytorch/libtorch ..
-- The C compiler identification is AppleClang 14.0.3.14030022
-- The CXX compiler identification is AppleClang 14.0.3.14030022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Torch: /Users/raulcardona/pytorch/libtorch/lib/libtorch.dylib  
-- Configuring done (0.7s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/raulcardona/pytorch/Projects/Basic_PyTorch_Project/build


### **_But when I run:_** 

❯ cmake --build . --config Release
[ 50%] Building CXX object CMakeFiles/example-app.dir/main.cpp.o
[100%] Linking CXX executable example-app
ld: warning: ignoring file /Users/raulcardona/pytorch/libtorch/lib/libc10.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /Users/raulcardona/pytorch/libtorch/lib/libtorch_cpu.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /Users/raulcardona/pytorch/libtorch/lib/libtorch.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /Users/raulcardona/pytorch/libtorch/lib/libc10.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /Users/raulcardona/pytorch/libtorch/lib/libkineto.a, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
Undefined symbols for architecture arm64:
  "at::_ops::rand::call(c10::ArrayRef<c10::SymInt>, c10::optional<c10::ScalarType>, c10::optional<c10::Layout>, c10::optional<c10::Device>, c10::optional<bool>)", referenced from:
      at::rand(c10::ArrayRef<long long>, c10::TensorOptions) in main.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 main.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 main.cpp.o
  "c10::UndefinedTensorImpl::_singleton", referenced from:
      c10::UndefinedTensorImpl::singleton() in main.cpp.o
  "c10::AutogradMetaInterface::~AutogradMetaInterface()", referenced from:
      torch::autograd::AutogradMeta::AutogradMeta(c10::TensorImpl*, bool, torch::autograd::Edge) in main.cpp.o
  "c10::impl::ExcludeDispatchKeyGuard::ExcludeDispatchKeyGuard(c10::DispatchKeySet)", referenced from:
      at::AutoDispatchBelowADInplaceOrView::AutoDispatchBelowADInplaceOrView() in main.cpp.o
  "c10::impl::ExcludeDispatchKeyGuard::~ExcludeDispatchKeyGuard()", referenced from:
      at::AutoDispatchBelowADInplaceOrView::~AutoDispatchBelowADInplaceOrView() in main.cpp.o
  "c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)", referenced from:
      c10::fromIntArrayRefSlow(c10::ArrayRef<long long>) in main.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 main.cpp.o
      c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, c10::detail::CompileTimeEmptyString) in main.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::intrusive_ptr_target::~intrusive_ptr_target() in main.cpp.o
      c10::Device::validate() in main.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 main.cpp.o
      c10::intrusive_ptr_target::~intrusive_ptr_target() in main.cpp.o
      c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::retain_() in main.cpp.o
      c10::ArrayRef<c10::SymInt>::debugCheckNullptrInvariant() in main.cpp.o
  "caffe2::TypeMeta::error_unsupported_typemeta(caffe2::TypeMeta)", referenced from:
      caffe2::TypeMeta::toScalarType() in main.cpp.o
  "vtable for c10::AutogradMetaInterface", referenced from:
      c10::AutogradMetaInterface::AutogradMetaInterface() in main.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 main.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[2]: *** [example-app] Error 1
make[1]: *** [CMakeFiles/example-app.dir/all] Error 2
make: *** [all] Error 2


### **This is my main program:**

#include <torch/torch.h>
#include <iostream>

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}


### **This is my CMakeLists.txt file:**

cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app main.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 17)



### Versions

zsh: command not found: wget
zsh: command not found: python

cc @malfet @seemethere @jbschlosser