Repository: pytorch
Issue: LNK2019 error when linking with MSVC [Caffe2]
Labels: caffe2

## Issue description
I've been trying to build a simple C++ program using the Caffe2 library (statically linked) since last week and it has been a painful experience. I couldn't find any information on how to properly build the Caffe2 library so I've had to figure it out through trial and error. I managed to compile and install the caffe2 lib with these commands (powershell):

```
> cd pytorch
> mkdir build
> cd build
> cmake ../.. -DCMAKE_INSTALL_PREFIX="${HOME}/caffe2-static-minimal" -DBUILD_PYTHON=OFF -DUSE_CUDA=OFF  -DUSE_GFLAGS=OFF -DUSE_GLOG=OFF -DUSE_GLOO=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_TEST=OFF -DBUILD_BINARY=OFF -DUSE_LMDB=OFF -DUSE_LEVELDB=OFF -DUSE_MPI=OFF -DUSE_OPENMP=OFF -DUSE_OPENCV=OFF -DBUILD_ATEN=ON -DCAFFE2_DISABLE_NUMA=1 -G "Visual Studio 15 2017 Win64"
> cmake --build . --config Release --target install
> # For some reason the caffe2_protos.lib and onnxifi_loader.lib files aren't installed, so I've to manually copy then:
> cp lib/Release/caffe2_protos.lib "${HOME}/caffe2-static-minimal/lib/"
> cp lib/Release/onnxifi_loader.lib "${HOME}/caffe2-static-minimal/lib/"
> # The protobuf library shouldn't start with 'lib' when building in a Windows enviroment:
> pushd "${HOME}/caffe2-static-minimal/lib/"
> mv libprotobuf.lib protobuf.lib
> popd
```
When I try to build the simple example listed bellow I get a bunch of link errors, MSVC output:
```
(Link target) -> 
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: __cdecl caffe2::TypeIdentifier::TypeIdentifier(unsigned short)" (__imp_??0TypeIdentifier@caffe2@@AEAA@G@Z) referenced in function "public: __cdecl caffe2::Tensor::Tensor<float>(class std::vector<__int64,class std::allocator<__int64> > const &,class std::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext *)" (??$?0M@Tensor@caffe2@@QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAVBaseContext@1@@Z) [C:\Users\Administrator\repos\test-caffe2\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: __cdecl caffe2::TypeMeta::TypeMeta(class caffe2::TypeIdentifier,unsigned __int64,void (__cdecl*)(void *,unsigned __int64),void (__cdecl*)(void const *,void *,unsigned __int64),void (__cdecl*)(void *,unsigned __int64))" (__imp_??0TypeMeta@caffe2@@AEAA@VTypeIdentifier@1@_KP6AXPEAX1@ZP6AXPEBX21@Z3@Z) referenced in function "public: __cdecl caffe2::Tensor::Tensor<float>(class std::vector<__int64,class std::allocator<__int64> > const &,class std::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext *)" (??$?0M@Tensor@caffe2@@QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAVBaseContext@1@@Z) [C:\Users\Administrator\repos\test-caffe2\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: unsigned __int64 const & __cdecl caffe2::TypeMeta::itemsize(void)const " (__imp_?itemsize@TypeMeta@caffe2@@QEBAAEB_KXZ) referenced in function "public: __cdecl caffe2::Tensor::Tensor<float>(class std::vector<__int64,class std::allocator<__int64> > const &,class std::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext *)" (??$?0M@Tensor@caffe2@@QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAVBaseContext@1@@Z) [C:\Users\Administrator\repos\test-caffe2\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caffe2::TypeMeta::ctor(void)const )(void *,unsigned __int64)" (__imp_?ctor@TypeMeta@caffe2@@QEBAP6AXPEAX_K@ZXZ) referenced in function "public: void * __cdecl caffe2::Tensor::raw_mutable_data(class caffe2::TypeMeta const &)" (?raw_mutable_data@Tensor@caffe2@@QEAAPEAXAEBVTypeMeta@2@@Z) [C:\Users\Administrator\repos\test-caffe2\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caffe2::TypeMeta::copy(void)const )(void const *,void *,unsigned __int64)" (__imp_?copy@TypeMeta@caffe2@@QEBAP6AXPEBXPEAX_K@ZXZ) referenced in function "public: __cdecl caffe2::Tensor::Tensor<float>(class std::vector<__int64,class std::allocator<__int64> > const &,class std::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext *)" (??$?0M@Tensor@caffe2@@QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAVBaseContext@1@@Z) [C:\Users\Administrator\repos\test-caffe2\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caffe2::TypeMeta::dtor(void)const )(void *,unsigned __int64)" (__imp_?dtor@TypeMeta@caffe2@@QEBAP6AXPEAX_K@ZXZ) referenced in function "public: void * __cdecl caffe2::Tensor::raw_mutable_data(class caffe2::TypeMeta const &)" (?raw_mutable_data@Tensor@caffe2@@QEAAPEAXAEBVTypeMeta@2@@Z) [C:\Users\Administrator\repos\test-caffe2\build\test-caffe2.vcxproj]
  C:\Users\Administrator\repos\test-caffe2\build\Release\test-caffe2.exe : fatal error LNK1120: 6 unresolved externals [C:\Users\Administrator\repos\test-caffe2\build\test-caffe2.vcxproj]

    45 Warning(s)
    7 Error(s)
```

I've tried applying  [#10652](https://github.com/pytorch/pytorch/pull/10652) and [#10765](https://github.com/pytorch/pytorch/pull/10765) but I've got the same results.

## Code example
Here is a minimal example to reproduce the link problems:

test-caffe2.cpp:
```
#include <caffe2/core/init.h>
#include <caffe2/core/net.h>
#include <caffe2/utils/proto_utils.h>

int main(int argc, char **argv) {
	caffe2::GlobalInit();

	std::vector<float> data;
	std::vector<caffe2::TIndex> dims({1, 3, 224, 224});
	caffe2::CPUContext context;
	volatile caffe2::Tensor tensor(dims, data, &context);

	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}
```

CMakeLists.txt:
```
cmake_minimum_required(VERSION 2.6)

project (caffe2_test)

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")

find_package(Threads)

set(ALL_LIBRARIES)
list(APPEND ALL_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})

find_library(CAFFE2_LIB caffe2)
list(APPEND ALL_LIBRARIES ${CAFFE2_LIB})

find_library(PROTOBUF_LIB protobuf)
list(APPEND ALL_LIBRARIES ${PROTOBUF_LIB})

find_library(ONNX_PROTO_LIB onnx_proto)
list(APPEND ALL_LIBRARIES ${ONNX_PROTO_LIB})

find_library(ONNX_LIB onnx)
list(APPEND ALL_LIBRARIES ${ONNX_LIB})

find_library(ONNXIFI_LOADER_LIB onnxifi_loader)
list(APPEND ALL_LIBRARIES ${ONNXIFI_LOADER_LIB})

find_library(NOMNIGRAPH_LIB nomnigraph)
list(APPEND ALL_LIBRARIES ${NOMNIGRAPH_LIB})

find_library(CAFFE2_PROTO_LIB caffe2_protos)
list(APPEND ALL_LIBRARIES ${CAFFE2_PROTO_LIB})

find_library(CPUINFO_LIB cpuinfo)
list(APPEND ALL_LIBRARIES ${CPUINFO_LIB})

find_library(CLOG_LIB clog)
list(APPEND ALL_LIBRARIES ${CLOG_LIB})

include_directories(${CAFFE2_INCLUDE_DIR})

if(NOT CAFFE2_LIB)
  message(FATAL_ERROR "Caffe2 lib not found")
endif()

set(CMAKE_CXX_STANDARD 11)

add_executable(test-caffe2 test-caffe2.cpp)

target_link_libraries(test-caffe2 ${ALL_LIBRARIES} ${CMAKE_DL_LIBS})

```
Build instructions (powershell):
```
> mkdir build
> cd build
> cmake ..  -DCMAKE_PREFIX_PATH="${HOME}/caffe2-static-minimal/lib" -DCAFFE2_INCLUDE_DIR="${HOME}/caffe2-static-minimal/include/" -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 15 2017 Win64"
> cmake --build . --config Release
```

Any hints?

## System Info
- PyTorch / Caffe2: c8b246abf31a8717105622077bc7669e2dc753a9 with [#10652](https://github.com/pytorch/pytorch/pull/10652) and [#10765](https://github.com/pytorch/pytorch/pull/10765) applyed
- Build command: ```cmake ../.. -DCMAKE_INSTALL_PREFIX="${HOME}/caffe2-static-minimal" -DBUILD_PYTHON=OFF -DUSE_CUDA=OFF  -DUSE_GFLAGS=OFF -DUSE_GLOG=OFF -DUSE_GLOO=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_TEST=OFF -DBUILD_BINARY=OFF -DUSE_LMDB=OFF -DUSE_LEVELDB=OFF -DUSE_MPI=OFF -DUSE_OPENMP=OFF -DUSE_OPENCV=OFF -DBUILD_ATEN=ON -DCAFFE2_DISABLE_NUMA=1 -G "Visual Studio 15 2017 Win64"```
- OS: Windows Server 2016 1607
- MSVC version: 19.15.26726.0
- CMake version: 3.11.18040201-MSVC_2