Linker failed with ambiguous references


(Geirrastad) #1

I am trying to use ROS2, OpenCV 3.4 and pytorch, but when theyall are in the cmake scope I get this error:

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<Device> 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<class T> 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<class _Tp> 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<Device> device_of(TensorList t) {
[build]         ^~~~~~~~
[build] In file included from /usr/local/libtorch/include/c10/core/TensorOptions.h:10:0,
[build]

Now, if i change /usr/local/libtorch/include/ATen/DeviceGuard.h and prefixes optional etc with either std:: or c10 it compiles …
When I only compile against libtorch I do not get this error.

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…


(Edward Z Yang) #2

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


(Geirrastad) #3

Hi,

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.