Failed to build libtorch because "Multiple definition of at::naive::DispatchStub"

username: Yizhi_Wang
cooked: <p>Hi, I want to build libtorch from source without cuda because the official binary is using old C++ABI. The steps i did is as follows:</p>
<ol>
<li>cd pytorch</li>
<li>mkdir build &amp;&amp; cd build</li>
<li>BUILD_TORCH=ON ONNX_NAMESPACE=onnx_torch bash …/tools/build_pytorch_libs.sh --use-nnpack caffe2</li>
</ol>
<p>And then i meet the following errors:</p>
<pre><code class="lang-auto">Linking CXX shared library ../lib/libcaffe2.so
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.AVX2.cpp.o:(.data.rel.local+0x0): multiple definition of `at::native::DispatchStub&lt;void (*)(at::Tensor&amp;, at::Tensor const&amp;), at::native::truncImpl&gt;::AVX2'
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.o:(.data.rel.local+0x0): first defined here
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.AVX2.cpp.o:(.data.rel.local+0x8): multiple definition of `at::native::DispatchStub&lt;void (*)(at::Tensor&amp;, at::Tensor const&amp;), at::native::tanhImpl&gt;::AVX2'
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.o:(.data.rel.local+0x8): first defined here
//many errors like above
//many errors like above
collect2: error: ld returned 1 exit status
caffe2/CMakeFiles/caffe2.dir/build.make:58764: recipe for target 'lib/libcaffe2.so' failed
make[2]: *** [lib/libcaffe2.so] Error 1
CMakeFiles/Makefile2:1305: recipe for target 'caffe2/CMakeFiles/caffe2.dir/all' failed
make[1]: *** [caffe2/CMakeFiles/caffe2.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
</code></pre>
<p>There are a lot of similar errors like this. According to the comments in <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/DispatchStub.h" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/DispatchStub.h</a>, this file is compiled for multiple times and then linked. But this information still cannot help me figure out how to fix it.</p>
<p>My environment:<br>
OS: Ubuntu18.04 LTS （Windows Linux Subsystem)<br>
Compiler: GCC7.3<br>
CMake: 3.12<br>
pytorch master: <a href="https://github.com/pytorch/pytorch/commit/eeb14675f12a19822d3a13f3d203c6396ee6cfdd" rel="nofollow noopener">eeb1467</a></p>
<p>Is there any idea? Thanks very much!!</p>

username: jinfagang
cooked: <p>I got different errors:</p>
<pre><code class="lang-auto">multiple definition of `omp_get_place_num'
FAILED: bin/dead_code_elim_test 
: &amp;&amp; /usr/bin/c++  -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -DHAVE_AVX_CPU_DEFINITION -DHAVE_AVX2_CPU_DEFINITION -O3  -rdynamic -L/usr/lib -pthread caffe2/CMakeFiles/dead_code_elim_test.dir/opt/dead_code_elim_test.cc.o  -o bin/dead_code_elim_test -L/media/jintain/sg/ai/libs/pytorch/third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib -Wl,-rpath,/media/jintain/sg/ai/libs/pytorch/third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib:/media/jintain/sg/ai/libs/pytorch/build_libtorch/build/lib:/usr/local/cuda/lib64: lib/libgtest_main.a -Wl,--no-as-needed,/media/jintain/sg/ai/libs/pytorch/build_libtorch/build/lib/libtorch.so -Wl,--as-needed lib/libprotobuf.a /usr/lib/x86_64-linux-gnu/libiomp5.so lib/libmkldnn.a -lmklml_intel ../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so lib/libc10_cuda.so lib/libc10.so /usr/local/cuda/lib64/libnvToolsExt.so /usr/local/cuda/lib64/libcudart.so /usr/local/cuda/lib64/libcufft.so /usr/local/cuda/lib64/libcurand.so /usr/local/cuda/lib64/libcudnn.so.7 /usr/local/cuda/lib64/libcublas.so lib/libgtest.a -lpthread &amp;&amp; :
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_num_places'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_place_nums'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_max_task_priority_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num_procs'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_num_places_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_proc_ids_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num_procs_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_place_nums_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_num_places_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_max_task_priority'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_num_places'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_proc_ids'
collect2: error: ld returned 1 exit status
[2297/2451] Linking CXX executable bin/event_gpu_test
</code></pre>

username: yf225
cooked: <p>Could you try a clean build of libtorch following <a href="https://gist.github.com/mhubii/1c1049fb5043b8be262259efac4b89d5" rel="nofollow noopener">https://gist.github.com/mhubii/1c1049fb5043b8be262259efac4b89d5</a> ?</p>