Repository: pytorch
Issue: Static Build of Libtorch not linking correctly
Labels: module: build,triaged

## 🐛 Bug

I downloaded the source of pytorch 1.9.0. And i built it with SHARED_LIBS off as follows to get a compact build

```
cmake -GNinja -DBUILD_TEST:BOOL=OFF -DUSE_CUDA:BOOL=ON -DUSE_CUDNN:BOOL=ON -DUSE_MAGMA:BOOL=OFF -DUSE_PTHREADPOOL:BOOL=OFF -DUSE_SOURCE_DEBUG_ON_MOBILE:BOOL=OFF -DCMAKE_BUILD_TYPE:STRING=Release -DPYTHON_EXECUTABLE:PATH=`which python3` -DUSE_KINETO:BOOL=OFF -DUSE_DISTRIBUTED:BOOL=OFF -DUSE_OPENMP:BOOL=OFF -DUSE_MKLDNN:BOOL=OFF -DUSE_NCCL:BOOL=OFF -DBUILD_PYTHON:BOOL=OFF -DUSE_QNNPACK:BOOL=OFF -DUSE_PYTORCH_QNNPACK:BOOL=OFF -DUSE_XNNPACK:BOOL=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX:PATH=../libtorch_v6 ../pytorch
```

I am able to see the following generated `.a` files, and I am able to do `ld *.a` on all the files (which likely isn't helpful for static libraries anyway but still)

![image](https://user-images.githubusercontent.com/85202628/137371283-101ff65b-dae7-436a-862c-81d3348cf7f5.png)

However, if I try to integrate it into my project, I get a whole bunch of linker errors. I have definitely included every single .a and .so file here:

```
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(Context.cpp.o):Context.cpp:function at::Context::setDefaultMobileCPUAllocator(): error: undefined reference to 'c10::GetCPUAllocator()'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(Context.cpp.o):Context.cpp:function at::Context::setDefaultMobileCPUAllocator(): error: undefined reference to 'c10::GetDefaultMobileCPUAllocator()'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(Context.cpp.o):Context.cpp:function at::Context::unsetDefaultMobileCPUAllocator(): error: undefined reference to 'c10::SetCPUAllocator(c10::Allocator*, unsigned char)'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(Context.cpp.o):Context.cpp:function at::Context::supportedQEngines(): error: undefined reference to 'fbgemm::fbgemmSupportedCPU()'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(Context.cpp.o):Context.cpp:function at::getCPUAllocator(): error: undefined reference to 'c10::GetCPUAllocator()'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(Context.cpp.o):Context.cpp:function at::Context::setDefaultMobileCPUAllocator(): error: undefined reference to 'c10::SetCPUAllocator(c10::Allocator*, unsigned char)'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(TensorIndexing.cpp.o):TensorIndexing.cpp:function c10::impl::InlineDeviceGuard<c10::impl::VirtualGuardImpl>::InlineDeviceGuard(c10::Device): error: undefined reference to 'c10::impl::device_guard_impl_registry'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(TensorIndexing.cpp.o):TensorIndexing.cpp:function c10::impl::InlineDeviceGuard<c10::impl::VirtualGuardImpl>::InlineDeviceGuard(c10::Device): error: undefined reference to 'c10::operator<<(std::ostream&, c10::DeviceType)'
external/libtorch_pro_cuda_cudnn_static/libtorch_v6/lib/libtorch_cpu.a(Tensor.cpp.o):Tensor.cpp:function c10::detail::_str_wrapper<char const*, c10::DeviceType const&, char const*>::call[abi:cxx11](char const* const&, c10::DeviceType const&, char const* const&): error: undefined reference to 'c10::operator<<(std::ostream&, c10::DeviceType)'

.. and many many more
```
https://pastebin.com/BGphM0BL

I don't get what I am doing wrong

cc @malfet @seemethere