title: Cannot link C++ using tensorflow C++ API tensorflow_cc.lib on Windows 10/Visual C++ 2017 -- undefined symbol r2.0
body: <p>I have a C++ program that I am trying to use with the tensorflow C++ API. I have it working fine on linux. But I cannot get the dumb thing to link on Windows. I am using the r2.0 branch of tensorflow. I am building tensorflow from source using Visual C++ 2017 (v14.16.27023). It took a while but I finally got bazel to build tensorflow_cc.dll and tensorflow_cc.lib. Linking my program to the tensorflow_cc.lib gave 3 undefined tensorflow symbols initially</p>
  
  <pre><code>?_TensorShapeProto_default_instance_@tensorflow@@3VTensorShapeProtoDefaultTypeInternal@1@A
  ??0SessionOptions@tensorflow@@QEAA@XZ
  ?LoadSavedModel@tensorflow@@YA?AVStatus@1@AEBUSessionOptions@1@AEBVRunOptions@1@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV?$unordered_set@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@6@QEAUSavedModelBundle@1@@Z
  </code></pre>
  
  <p>I read some github bug reports and discovered that there is a complex method used to export a subset of the global symbols into the .lib file that involves a python program tensorflow/tools/def_file_filter/def_file_filter.py.tpl and someone showed how to put extra symbols in that to add to the set exported. I went ahead and did that:</p>
  
  <pre><code>diff --git a/tensorflow/tools/def_file_filter/def_file_filter.py.tpl ...
  @@ -154,6 +154,9 @@ def main():
         else:
           def_fp.write("\t" + decorated + " DATA\n")
         taken.add(decorated)
  +    def_fp.write("\t??0SessionOptions@tensorflow@@QEAA@XZ\n")
  +    def_fp.write("\t?_TensorShapeProto_default_instance_@tensorflow@@3VTensorShapeProtoDefaultTypeInternal@1@A DATA\n")
  +    def_fp.write("\t?LoadSavedModel@tensorflow@@YA?AVStatus@1@AEBUSessionOptions@1@AEBVRunOptions@1@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV?$unordered_set@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@6@QEAUSavedModelBundle@1@@Z\n")
       def_fp.close()
  
     exit_code = proc.wait()
  </code></pre>
  
  <p>I then re-bazeled tensorflow_cc.lib and it fixed TWO of the THREE. I then discovered that the one that remained undefined upon link was actually already exported:</p>
  
  <pre><code>?_TensorShapeProto_default_instance_@tensorflow@@3VTensorShapeProtoDefaultTypeInternal@1@A
  </code></pre>
  
  <p>So I actually removed it from my changes to def_file_filter.py.tpl. That particular symbol is defined in a file generated from a .proto file into bazel-out/x64_windows-opt/bin/tensorflow/core/framework/tensor_shape.pb.cc. It is a global scope class instance:</p>
  
  <pre><code>static void InitDefaultsscc_info_TensorShapeProto_tensorflow_2fcore_2fframework_2ftensor_5fshape_2eproto() {
    GOOGLE_PROTOBUF_VERIFY_VERSION;
  
    {
      void* ptr = &amp;::tensorflow::_TensorShapeProto_default_instance_;
      new (ptr) ::tensorflow::TensorShapeProto();
      ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr);
    }
    ::tensorflow::TensorShapeProto::InitAsDefaultInstance();
  }
  </code></pre>
  
  <p>I looked at the symbols in tensorflow_cc.lib and did find an entry related to this symbol:</p>
  
  <pre><code>tensorflow_cc.dll:
  0000000000000000 I .idata$4
  0000000000000000 I .idata$5
  0000000000000000 I .idata$6
                   U __IMPORT_DESCRIPTOR_tensorflow_cc
  0000000000000000 I __imp_?_TensorShapeProto_default_instance_@tensorflow@@3VTensorShapeProtoDefaultTypeInternal@1@A
  </code></pre>
  
  <p>I am not sure if it correct or not. Presumably not since I am still getting:</p>
  
  <pre><code>link tfseg.dll .wsl.obj/plugin.obj .wsl.obj/segTask.obj .wsl.obj/tfseg....
     Creating library C:/root/ispace/plugins/tfseg/bin_release/tfseg.lib and object C:/root/ispace/plugins/tfseg/bin_release/tfseg.exp
  tfseg.obj : error LNK2001: unresolved external symbol "class tensorflow::TensorShapeProtoDefaultTypeInternal tensorflow::_TensorShapeProto_default_instance_" (?_TensorShapeProto_default_instance_@tensorflow@@3VTensorShapeProtoDefaultTypeInternal@1@A)
  C:/root/ispace/plugins/tfseg/bin_release/tfseg.dll : fatal error LNK1120: 1 unresolved externals
  </code></pre>
  
  <p>I am at my wits end and very frustrated. Anyone seen this before? Any ideas?</p>
  
  <pre><code>
  </code></pre>