Repository: pytorch
Issue: built from source windows static library with multiple "unresolved external symbol"
Labels: module: build,module: windows,triaged

### 🐛 Describe the bug

We are building the static libtorch library from source (`release/1.12` branch, we also tried the `master` branch. both of them have the identical issues), we are using the conda environment (python 3.7).
After following the installation guide at `https://github.com/pytorch/pytorch/blob/master/README.md`, installing all the cuda, cudnn dependencies, we set out to build the library.

Here's how we build the library
``` bash
set BUILD_SHARED_LIBS=OFF
set USE_NINJA=OFF
set CMAKE_GENERATOR=Visual Studio 16 2019
set BUILD_TEST=False
python ../tools/build_libtorch.py
```

It builds fine, but when we link it into a visual studio project to run a demo code, it has the following error msgs.
The code demo:
``` cpp

#include <iostream>
#include <ATen/ATen.h>
#include <torch/torch.h>

int main() {
    torch::Tensor tensor = torch::rand({ 2, 3 });

    std::cout << torch::cuda::is_available() << std::endl;
    torch::Tensor tensor2 = at::tensor({ -1, 1 }, at::kCUDA);
    std::cout << tensor2 << std::endl;
    std::cout << tensor << std::endl;
    return 0;
}
```

Error messages:
```
1>C:\Users\tingwuw\pytorch\build_libtorch\libtorch-install\include\ATen\TensorIterator.h(541): message : see reference to class template instantiation 'c10::SmallVector<at::OperandInfo,4>' being compiled
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "private: static void __cdecl caffe2::TypeMeta::error_unsupported_typemeta(class caffe2::TypeMeta)" (?error_unsupported_typemeta@TypeMeta@caffe2@@CAXV12@@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl at::print(class std::basic_ostream<char,struct std::char_traits<char> > &,class at::Tensor const &,__int64)" (?print@at@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV23@AEBVTensor@1@_J@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: static class at::Tensor __cdecl at::_ops::rand::call(class c10::ArrayRef<__int64>,class c10::optional<enum c10::ScalarType>,class c10::optional<enum c10::Layout>,class c10::optional<struct c10::Device>,class c10::optional<bool>)" (?call@rand@_ops@at@@SA?AVTensor@3@V?$ArrayRef@_J@c10@@V?$optional@W4ScalarType@c10@@@6@V?$optional@W4Layout@c10@@@6@V?$optional@UDevice@c10@@@6@V?$optional@_N@6@@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "class at::Tensor __cdecl at::tensor(class c10::ArrayRef<int>,struct c10::TensorOptions const &)" (?tensor@at@@YA?AVTensor@1@V?$ArrayRef@H@c10@@AEBUTensorOptions@4@@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: void __cdecl c10::TensorImpl::set_autograd_meta(class std::unique_ptr<struct c10::AutogradMetaInterface,struct std::default_delete<struct c10::AutogradMetaInterface> >)" (?set_autograd_meta@TensorImpl@c10@@QEAAXV?$unique_ptr@UAutogradMetaInterface@c10@@U?$default_delete@UAutogradMetaInterface@c10@@@std@@@std@@@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: virtual __cdecl c10::AutogradMetaInterface::~AutogradMetaInterface(void)" (??1AutogradMetaInterface@c10@@UEAA@XZ)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: __cdecl c10::impl::ExcludeDispatchKeyGuard::~ExcludeDispatchKeyGuard(void)" (??1ExcludeDispatchKeyGuard@impl@c10@@QEAA@XZ)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: __cdecl c10::impl::ExcludeDispatchKeyGuard::ExcludeDispatchKeyGuard(class c10::DispatchKeySet)" (??0ExcludeDispatchKeyGuard@impl@c10@@QEAA@VDispatchKeySet@2@@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "protected: void __cdecl c10::SmallVectorBase<unsigned int>::grow_pod(void *,unsigned __int64,unsigned __int64)" (?grow_pod@?$SmallVectorBase@I@c10@@IEAAXPEAX_K1@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "private: static struct c10::UndefinedTensorImpl c10::UndefinedTensorImpl::_singleton" (?_singleton@UndefinedTensorImpl@c10@@0U12@A)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "void __cdecl c10::detail::torchInternalAssertFail(char const *,char const *,unsigned int,char const *,char const *)" (?torchInternalAssertFail@detail@c10@@YAXPEBD0I00@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "void __cdecl c10::detail::torchInternalAssertFail(char const *,char const *,unsigned int,char const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?torchInternalAssertFail@detail@c10@@YAXPEBD0I0AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "void __cdecl c10::detail::torchCheckFail(char const *,char const *,unsigned int,char const *)" (?torchCheckFail@detail@c10@@YAXPEBD0I0@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "bool __cdecl torch::cuda::is_available(void)" (?is_available@cuda@torch@@YA_NXZ)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl torch::autograd::AutogradMeta::set_fw_grad(class at::TensorBase const &,class at::TensorBase const &,unsigned __int64,bool)" (?set_fw_grad@AutogradMeta@autograd@torch@@UEAAXAEBVTensorBase@at@@0_K_N@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: virtual class at::Tensor const & __cdecl torch::autograd::AutogradMeta::fw_grad(unsigned __int64,class at::TensorBase const &)const " (?fw_grad@AutogradMeta@autograd@torch@@UEBAAEBVTensor@at@@_KAEBVTensorBase@5@@Z)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: static class std::shared_ptr<struct torch::autograd::ForwardADLevel> __cdecl torch::autograd::ForwardADLevel::try_get_by_idx(unsigned __int64)" (?try_get_by_idx@ForwardADLevel@autograd@torch@@SA?AV?$shared_ptr@UForwardADLevel@autograd@torch@@@std@@_K@Z)
1>C:\Users\tingwuw\source\repos\ConsoleApplication1\x64\Release\ConsoleApplication1.exe : fatal error LNK1120: 17 unresolved externals
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

```


My colleague and I have tried different machines also tried different building methods on separate machines,
and the error messages are consistent though different setup.
Also, we can build the dynamic library and use it successfully for the example code. Only the static one does not work.

No sure what's happening. Is there anything missing that could be helpful?
Let me know and I can update the info asap :)

### Versions

Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 11 Enterprise
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 3.24.1
Libc version: N/A

Python version: 3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.22621-SP0
Is CUDA available: N/A
CUDA runtime version: 11.5.50
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090 Ti
Nvidia driver version: 512.16
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

Versions of relevant libraries:
[pip3] numpy==1.21.5
[conda] blas                      1.0                         mkl
[conda] mkl                       2021.4.0           haa95532_640
[conda] mkl-include               2022.1.0           haa95532_193
[conda] mkl-service               2.4.0            py37h2bbff1b_0
[conda] mkl_fft                   1.3.1            py37h277e83a_0
[conda] mkl_random                1.2.2            py37hf11a4ad_0
[conda] numpy                     1.21.5           py37h7a0a035_3
[conda] numpy-base                1.21.5           py37hca35cd5_3

cc @malfet @seemethere @peterjc123 @mszhanyi @skyline75489 @nbcsm