Unresolved external symbol (LNK2019) for c10 in Windows

I tried to build a Visual Studio sample project using LibTorch but got LNK2019 unresolved external associated with the c10 library.

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: __cdecl c10::WeakOrStrongTypePtr::WeakOrStrongTypePtr(struct c10::WeakTypePtr)" (__imp_??0WeakOrStrongTypePtr@c10@@QEAA@UWeakTypePtr@1@@Z) referenced in function "public: void __cdecl c10::ivalue::Object::unsafe_make_weak_compilation_ref(void)" (?unsafe_make_weak_compilation_ref@Object@ivalue@c10@@QEAAXXZ)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK1120	14 unresolved externals	TorchProject	C:\Users\user\test_project\TorchProject\x64\Release\TorchProject.exe	1	
Error	LNK2001	unresolved external symbol "class at::Tensor __cdecl at::native::_torch_cuda_cu_linker_symbol_op_cuda(class at::Tensor const &)" (?_torch_cuda_cu_linker_symbol_op_cuda@native@at@@YA?AVTensor@2@AEBV32@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\LINK	1	
Error	LNK2001	unresolved external symbol "public: __cdecl c10::WeakOrStrongTypePtr::~WeakOrStrongTypePtr(void)" (??1WeakOrStrongTypePtr@c10@@QEAA@XZ)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: bool __cdecl c10::WeakOrStrongTypePtr::holds_strong_ref(void)const " (__imp_?holds_strong_ref@WeakOrStrongTypePtr@c10@@QEBA_NXZ) referenced in function "public: class std::shared_ptr<struct torch::jit::CompilationUnit> __cdecl c10::ivalue::Object::compilation_unit(void)" (?compilation_unit@Object@ivalue@c10@@QEAA?AV?$shared_ptr@UCompilationUnit@jit@torch@@@std@@XZ)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: static class std::shared_ptr<struct c10::TensorType> const & __cdecl c10::TensorType::get(void)" (__imp_?get@TensorType@c10@@SAAEBV?$shared_ptr@UTensorType@c10@@@std@@XZ) referenced in function "public: __cdecl c10::IValue::IValue<class at::Tensor,0>(class std::vector<class at::Tensor,class std::allocator<class at::Tensor> > const &)" (??$?0VTensor@at@@$0A@@IValue@c10@@QEAA@AEBV?$vector@VTensor@at@@V?$allocator@VTensor@at@@@std@@@std@@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: struct c10::IValue const & __cdecl c10::ivalue::TupleElements::at(unsigned __int64)const " (__imp_?at@TupleElements@ivalue@c10@@QEBAAEBUIValue@3@_K@Z) referenced in function "void __cdecl draw_images(struct c10::IValue,class cv::Mat,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?draw_images@@YAXUIValue@c10@@VMat@cv@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: struct c10::WeakOrStrongTypePtr & __cdecl c10::WeakOrStrongTypePtr::operator=(struct c10::WeakOrStrongTypePtr &&)" (__imp_??4WeakOrStrongTypePtr@c10@@QEAAAEAU01@$$QEAU01@@Z) referenced in function "public: struct c10::ivalue::Object & __cdecl c10::ivalue::Object::operator=(struct c10::ivalue::Object &&)" (??4Object@ivalue@c10@@QEAAAEAU012@$$QEAU012@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: struct c10::WeakOrStrongTypePtr & __cdecl c10::WeakOrStrongTypePtr::operator=(struct c10::WeakOrStrongTypePtr const &)" (__imp_??4WeakOrStrongTypePtr@c10@@QEAAAEAU01@AEBU01@@Z) referenced in function "public: struct c10::ivalue::Object & __cdecl c10::ivalue::Object::operator=(struct c10::ivalue::Object const &)" (??4Object@ivalue@c10@@QEAAAEAU012@AEBU012@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: struct c10::WeakTypePtr __cdecl c10::WeakOrStrongTypePtr::asWeakTypePtr(void)const " (__imp_?asWeakTypePtr@WeakOrStrongTypePtr@c10@@QEBA?AUWeakTypePtr@2@XZ) referenced in function "public: void __cdecl c10::ivalue::Object::unsafe_make_weak_compilation_ref(void)" (?unsafe_make_weak_compilation_ref@Object@ivalue@c10@@QEAAXXZ)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: __cdecl c10::IValue::IValue(class at::TensorBase)" (__imp_??0IValue@c10@@QEAA@VTensorBase@at@@@Z) referenced in function "public: __cdecl c10::IValue::IValue<class at::Tensor,0>(class std::vector<class at::Tensor,class std::allocator<class at::Tensor> > const &)" (??$?0VTensor@at@@$0A@@IValue@c10@@QEAA@AEBV?$vector@VTensor@at@@V?$allocator@VTensor@at@@@std@@@std@@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: __cdecl c10::WeakOrStrongTypePtr::WeakOrStrongTypePtr(struct c10::StrongTypePtr)" (__imp_??0WeakOrStrongTypePtr@c10@@QEAA@UStrongTypePtr@1@@Z) referenced in function "public: __cdecl c10::ivalue::Object::Object(struct c10::StrongTypePtr,unsigned __int64)" (??0Object@ivalue@c10@@QEAA@UStrongTypePtr@2@_K@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: __cdecl c10::WeakOrStrongTypePtr::WeakOrStrongTypePtr(struct c10::WeakOrStrongTypePtr &&)" (__imp_??0WeakOrStrongTypePtr@c10@@QEAA@$$QEAU01@@Z) referenced in function "public: __cdecl c10::ivalue::Object::Object(struct c10::ivalue::Object &&)" (??0Object@ivalue@c10@@QEAA@$$QEAU012@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: __cdecl c10::WeakOrStrongTypePtr::WeakOrStrongTypePtr(struct c10::WeakOrStrongTypePtr const &)" (__imp_??0WeakOrStrongTypePtr@c10@@QEAA@AEBU01@@Z) referenced in function "public: __cdecl c10::ivalue::Object::Object(struct c10::ivalue::Object const &)" (??0Object@ivalue@c10@@QEAA@AEBU012@@Z)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: __cdecl c10::WeakOrStrongTypePtr::~WeakOrStrongTypePtr(void)" (__imp_??1WeakOrStrongTypePtr@c10@@QEAA@XZ) referenced in function "int `public: __cdecl c10::ivalue::Object::Object(struct ivalue::Object::dtor$1 const &)'::`1'::dtor$1" (?dtor$1@?0???0Object@ivalue@c10@@QEAA@AEBU012@@Z@4HA)	TorchProject	C:\Users\user\test_project\TorchProject\example-app.obj	1	

Libtorch - libtorch-win-shared-with-deps-1.12.1+cu113
Cuda - 11.3

include directories

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\include
C:\Program Files\NVIDIA Corporation\NvToolsExt\include
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\include
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\include\torch\csrc\api\include
C:\Program Files (x86)\torchvision\include
C:\OpenCV\include

Additional library

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\lib\x64
C:\Program Files\NVIDIA Corporation\NvToolsExt\lib\x64
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\asmjit.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\c10.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\c10_cuda.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\caffe2_nvrtc.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\cpuinfo.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\fbgemm.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\kineto.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\libprotobuf.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\libprotobuf-lite.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\libprotoc.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\pthreadpool.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\torch.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\torch_cpu.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\torch_cuda.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\torch_cuda_cpp.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\torch_cuda_cu.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib\XNNPACK.lib
C:\libtorch-win-shared-with-deps-1.12.1+cu113\libtorch\lib
C:\Program Files (x86)\torchvision\lib
C:\OpenCV\x64\vc16\lib

I have linked all the libraries as shown above in Visual Studio. What can be the issue here?

  • If you are using visual-studio-extension (Installing C++ Distributions of PyTorch — PyTorch master documentation) for the project, all the required libraries (LibTorch, CUDA, vision etc.) need to be added to Properties → Linker → Input → Additional Dependencies. Adding the lib folder to ‘Properties → Linker → Additional Library Directories’ didn’t work for me.

  • The version worked for me - CUDA- 11.6 , LibTorch - libtorch-win-shared-with-deps-1.13.0+cu116.