Linker failed with ambiguous references

geirrastad: <p>I am trying to use ROS2, OpenCV 3.4 and pytorch, but when theyall are in the cmake scope I get this error:</p>
<pre><code class="lang-auto">In file included from /usr/local/libtorch/include/ATen/ATen.h:7:0,
[build]                  from /usr/local/libtorch/include/torch/csrc/api/include/torch/types.h:3,
[build]                  from /usr/local/libtorch/include/torch/script.h:3,
[build]                  from /home/geir/src/iaam_lib/standalone/../cnn/src/include/cnnmodule/cnn-module.h:5,
[build]                  from /home/geir/src/iaam_lib/standalone/../iaam/core/ObjectDetector.h:5,
[build]                  from /home/geir/src/iaam_lib/standalone/Main.cpp:8:
[build] /usr/local/libtorch/include/ATen/DeviceGuard.h:17:8: error: reference to ‘optional’ is ambiguous
[build]  inline optional&lt;Device&gt; device_of(Tensor t) {
[build]         ^~~~~~~~
[build] In file included from /usr/local/libtorch/include/c10/core/TensorOptions.h:10:0,
[build]                  from /usr/local/libtorch/include/ATen/core/Type.h:15,
[build]                  from /usr/local/libtorch/include/ATen/Type.h:2,
[build]                  from /usr/local/libtorch/include/ATen/Context.h:4,
[build]                  from /usr/local/libtorch/include/ATen/ATen.h:5,
[build]                  from /usr/local/libtorch/include/torch/csrc/api/include/torch/types.h:3,
[build]                  from /usr/local/libtorch/include/torch/script.h:3,
[build]                  from /home/geir/src/iaam_lib/standalone/../cnn/src/include/cnnmodule/cnn-module.h:5,
[build]                  from /home/geir/src/iaam_lib/standalone/../iaam/core/ObjectDetector.h:5,
[build]                  from /home/geir/src/iaam_lib/standalone/Main.cpp:8:
[build] /usr/local/libtorch/include/c10/util/Optional.h:118:7: note: candidates are: template&lt;class T&gt; class c10::optional
[build]  class optional;
[build]        ^~~~~~~~
[build] In file included from /usr/include/c++/7/bits/node_handle.h:39:0,
[build]                  from /usr/include/c++/7/bits/hashtable.h:37,
[build]                  from /usr/include/c++/7/unordered_map:47,
[build]                  from /usr/include/c++/7/functional:60,
[build]                  from /usr/local/opencv-3.4/include/opencv2/core/utility.hpp:60,
[build]                  from /usr/local/opencv-3.4/include/opencv2/core.hpp:3281,
[build]                  from /home/geir/src/iaam_lib/standalone/../iaam/core/FrameCollector.h:3,
[build]                  from /home/geir/src/iaam_lib/standalone/Main.cpp:1:
[build] /usr/include/c++/7/optional:57:11: note:                 template&lt;class _Tp&gt; class std::optional
[build]      class optional;
[build]            ^~~~~~~~
[build] In file included from /usr/local/libtorch/include/ATen/ATen.h:7:0,
[build]                  from /usr/local/libtorch/include/torch/csrc/api/include/torch/types.h:3,
[build]                  from /usr/local/libtorch/include/torch/script.h:3,
[build]                  from /home/geir/src/iaam_lib/standalone/../cnn/src/include/cnnmodule/cnn-module.h:5,
[build]                  from /home/geir/src/iaam_lib/standalone/../iaam/core/ObjectDetector.h:5,
[build]                  from /home/geir/src/iaam_lib/standalone/Main.cpp:8:
[build] /usr/local/libtorch/include/ATen/DeviceGuard.h:28:8: error: reference to ‘optional’ is ambiguous
[build]  inline optional&lt;Device&gt; device_of(TensorList t) {
[build]         ^~~~~~~~
[build] In file included from /usr/local/libtorch/include/c10/core/TensorOptions.h:10:0,
[build]
</code></pre>
<p>Now, if i change /usr/local/libtorch/include/ATen/DeviceGuard.h and prefixes optional etc with either std:: or c10 it compiles …<br>
When I only compile against libtorch I do not get this error.</p>
<p>I am not a libtorch man, I just try to stich some modules together, and wonder if anyone else hav had this problem. And if it is a problem or just me…</p>

ezyang: <p>Ugh, that sucks. Do you have a <code>using namespace std</code> somewhere in your code? If you can remove it (or move it after all of the torch imports) that should fix it.</p>

geirrastad: <p>Hi,</p>
<p>thx you were right! I imported a library that had using ns std! I totally overlooked this as I never use it in headers myself.</p>

jinfagang: <p>Can you share your cmakelist in ROS?</p>
<p>I can not link torch comfortably in ROS!!</p>

QueensGambit: <p>Hello <a class="mention" href="/u/geirrastad">@geirrastad</a>,</p>
<p>I created a PR which should fix this issue:<br>
</p><aside class="onebox githubpullrequest">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/pull/45736" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Pull Request">
    <svg width="60" height="60" class="github-icon" viewBox="0 0 12 16" aria-hidden="true"><path d="M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/pull/45736" target="_blank" rel="noopener nofollow ugc">Added missing namespaces for C++ API</a>
    </h4>

    <div class="branches">
      <code>pytorch:master</code> ← <code>QueensGambit:c++_api</code>
    </div>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-10-02" data-time="14:17:09" data-timezone="UTC">02:17PM - 02 Oct 20 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/QueensGambit" target="_blank" rel="noopener nofollow ugc">
          <img alt="QueensGambit" src="https://avatars1.githubusercontent.com/u/15967624?v=4" class="onebox-avatar-inline" width="20" height="20">
          QueensGambit
        </a>
      </div>

      <div class="lines" title="1 commits changed 3 files with 9 additions and 9 deletions">
        <a href="https://github.com/pytorch/pytorch/pull/45736/files" target="_blank" rel="noopener nofollow ugc">
          <span class="added">+9</span>
          <span class="removed">-9</span>
        </a>
      </div>
    </div>

  </div>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Best,<br>
Johannes</p>

Tom_Notch: <p>Execute this to patch source code of libtorch:</p>
<pre><code class="lang-auto">perl -pi -e 's/(?&lt;!c10::)optional&lt;/c10::optional&lt;/g; s/(?&lt;!c10::)nullopt/c10::nullopt/g' ./libtorch/include/ATen/ops/from_blob.h ./libtorch/include/ATen/DeviceGuard.h
</code></pre>

lskook47: <p><a class="mention" href="/u/ezyang">@ezyang</a> Thx, your advice saved my day!</p>