Minimal Pytorch C++ example failing at link time on Mac OS Mohave

ssaroha: <p>Hi<br>
I wanted to try out the torchscript examples on my local macOS Mohave 10.14.6. As part of that I am installing the C++ libtorch using the instructions here: <a href="https://pytorch.org/cppdocs/installing.html" class="inline-onebox" rel="noopener nofollow ugc">Installing C++ Distributions of PyTorch — PyTorch main documentation</a></p>
<p>Below is the sequence of commands I ran after downloading torchlib from <a href="https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip" rel="noopener nofollow ugc">https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip</a> and unzipping it in /Users/satender/pytorch/libtorch</p>
<pre><code class="lang-auto">(base) satender-mbp:example-app satender$ mkdir build
(base) satender-mbp:example-app satender$ cd build/
(base) satender-mbp:build satender$ cmake -DCMAKE_PREFIX_PATH=/Users/satender/pytorch/libtorch ..
-- The C compiler identification is AppleClang 11.0.0.11000033
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode_11.2.1_fb.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode_11.2.1_fb.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Found Torch: /Users/satender/pytorch/libtorch/lib/libtorch.so  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/satender/pytorch/example-app/build
(base) satender-mbp:build satender$ cmake --build . --config Release
Scanning dependencies of target example-app
[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o
[100%] Linking CXX executable example-app
ld: unknown option: --no-as-needed
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [example-app] Error 1
make[1]: *** [CMakeFiles/example-app.dir/all] Error 2
make: *** [all] Error 2

</code></pre>
<p>Looks like the OSX linker doesn’t support this option?<br>
would appreciate any help here.</p>
<p>Other thing I tried was remove the --no-as-needed options from this place</p>
<blockquote>
<p>(base) satender-mbp:example-app satender$ cat ./build/CMakeFiles/example-app.dir/link.txt<br>
/Applications/Xcode_11.2.1_fb.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -isysroot /Applications/Xcode_11.2.1_fb.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -mmacosx-version-min=10.14 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/example-app.dir/example-app.cpp.o -o example-app  /Users/satender/pytorch/libtorch/lib/libtorch.so /Users/satender/pytorch/libtorch/lib/libc10.so -Wl,–no-as-needed,/Users/satender/pytorch/libtorch/lib/libtorch_cpu.so -Wl,–as-needed /Users/satender/pytorch/libtorch/lib/libc10.so -Wl,–no-as-needed,/Users/satender/pytorch/libtorch/lib/libtorch.so -Wl,–as-needed</p>
</blockquote>
<p>removing both --as-needed and --no-as-needed references from above file and doing a make clean and make, results in the following error.</p>
<pre><code class="lang-auto">(base) satender-mbp:build satender$ make clean
(base) satender-mbp:build satender$ make
[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o
[100%] Linking CXX executable example-app
ld: warning: ignoring file /Users/satender/pytorch/libtorch/lib/libtorch.so, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 )
ld: warning: ignoring file /Users/satender/pytorch/libtorch/lib/libc10.so, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 )
ld: warning: ignoring file /Users/satender/pytorch/libtorch/lib/libc10.so, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 )
ld: warning: ignoring file /Users/satender/pytorch/libtorch/lib/libtorch.so, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 )
ld: warning: ignoring file /Users/satender/pytorch/libtorch/lib/libtorch_cpu.so, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 )
Undefined symbols for architecture x86_64:
  "at::rand(c10::ArrayRef&lt;long long&gt;, c10::TensorOptions const&amp;)", referenced from:
      torch::rand(c10::ArrayRef&lt;long long&gt;, c10::TensorOptions const&amp;)::'lambda'()::operator()() const in example-app.cpp.o
  "at::print(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;, at::Tensor const&amp;, long long)", referenced from:
      at::operator&lt;&lt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;, at::Tensor const&amp;) in example-app.cpp.o
  "c10::TensorImpl::set_autograd_meta(std::__1::unique_ptr&lt;c10::AutogradMetaInterface, std::__1::default_delete&lt;c10::AutogradMetaInterface&gt; &gt;)", referenced from:
      torch::autograd::make_variable(at::Tensor, bool, bool) in example-app.cpp.o
  "c10::UndefinedTensorImpl::_singleton", referenced from:
      c10::UndefinedTensorImpl::singleton() in example-app.cpp.o
  "c10::AutogradMetaInterface::~AutogradMetaInterface()", referenced from:
      torch::autograd::AutogradMeta::AutogradMeta(c10::TensorImpl*, bool, torch::autograd::Edge) in example-app.cpp.o
      torch::autograd::AutogradMeta::~AutogradMeta() in example-app.cpp.o
...
</code></pre>
<p>Any pointers on how to proceed?</p>

Raul-Cardona: <p><a class="mention" href="/u/ssaroha">@ssaroha</a> did you ever find a solution for this? Getting the exact same thing. The second set of errors.</p>