Repository: pytorch
Issue: Issues linking to libtorch on M2 mac
Labels: triaged,module: arm

### 🐛 Describe the bug

I am following the minimal example for compiling libtorch provided here: https://pytorch.org/cppdocs/installing.html
I am using libtorch for mac downloaded from here: https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-2.5.1.zip
This looks like it should be for arm64 and is the latest version from the PyTorch homepage.

I can run CMake fine, but am encountering issues when it comes to building and linking the c++.

```
[user@Mac build]$ cmake --build . --config Release
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[100%] Linking CXX executable example
Undefined symbols for architecture arm64:
  "at::_ops::rand::call(c10::ArrayRef<c10::SymInt>, std::optional<c10::ScalarType>, std::optional<c10::Layout>, std::optional<c10::Device>, std::optional<bool>)", referenced from:
      _main in example.cpp.o
  "at::print(std::ostream&, at::Tensor const&, long long)", referenced from:
      _main in example.cpp.o
  "c10::TensorImpl::set_autograd_meta(std::unique_ptr<c10::AutogradMetaInterface, std::default_delete<c10::AutogradMetaInterface>>)", referenced from:
      torch::autograd::make_variable(at::Tensor, bool, bool) in example.cpp.o
      torch::autograd::make_variable(at::Tensor, bool, bool) in example.cpp.o
      torch::autograd::make_variable(at::Tensor, bool, bool) in example.cpp.o
      torch::autograd::make_variable(at::Tensor, bool, bool) in example.cpp.o
  "c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)", referenced from:
      c10::fromIntArrayRefSlow(c10::ArrayRef<long long>) in example.cpp.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
make[2]: *** [example] Error 1
make[1]: *** [CMakeFiles/example.dir/all] Error 2
make: *** [all] Error 2
```

This seems to be similar to what happened when there was no arm binary available and we had to build libtorch from source (see #110810), but more recently binaries have been available and worked fine.

I have been looking through various issues and can't find anything similar, so apolofied if I've missed something, but to my knowledge I am just following the example from the website.

Note that this is using gcc-14 installed via homebrew which has worked fine in the past.
If I try and build without specifying a CXX or C compiler it defaults to AppleClang 16.0.0.16000026 and I get a different error of:
```
[jwa34@Mac build]$ cmake --build . --config Release
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
In file included from /Users/jwa34/libtorch_test/example.cpp:1:
In file included from /Users/jwa34/libtorch_test/libtorch/include/torch/csrc/api/include/torch/torch.h:3:
In file included from /Users/jwa34/libtorch_test/libtorch/include/torch/csrc/api/include/torch/all.h:7:
In file included from /Users/jwa34/libtorch_test/libtorch/include/torch/csrc/api/include/torch/autograd.h:3:
In file included from /Users/jwa34/libtorch_test/libtorch/include/torch/csrc/autograd/autograd.h:3:
In file included from /Users/jwa34/libtorch_test/libtorch/include/torch/csrc/autograd/variable.h:6:
In file included from /Users/jwa34/libtorch_test/libtorch/include/torch/csrc/autograd/cpp_hook.h:2:
In file included from /Users/jwa34/libtorch_test/libtorch/include/torch/csrc/autograd/function_hook.h:3:
In file included from /Users/jwa34/libtorch_test/libtorch/include/ATen/Tensor.h:3:
In file included from /Users/jwa34/libtorch_test/libtorch/include/ATen/core/Tensor.h:3:
In file included from /Users/jwa34/libtorch_test/libtorch/include/ATen/core/TensorBody.h:11:
In file included from /Users/jwa34/libtorch_test/libtorch/include/c10/core/Device.h:3:
/Users/jwa34/libtorch_test/libtorch/include/c10/core/DeviceType.h:10:10: fatal error: 'cstddef' file not found
   10 | #include <cstddef>
      |          ^~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/example.dir/example.cpp.o] Error 1
make[1]: *** [CMakeFiles/example.dir/all] Error 2
make: *** [all] Error 2
```
I have seen this sort of thing before where the default AppleClang becomes out of date, hence my decision to use gcc.

Note further that this behaviour was observed first in a larger project, but I decided to use the minimal example to try and pin down where things were going wrong.
The same error is occuring in the CI for that larger project and can be seen at e.g. https://github.com/Cambridge-ICCS/FTorch/actions/runs/12399866549/job/34615725295?pr=164

Many thanks for your assistance.

### Versions

Using libtorch 2.5.1 downloaded from https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-2.5.1.zip

cc @malfet @snadampal @milpuz01