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

Hi
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: Installing C++ Distributions of PyTorch — PyTorch main documentation

Below is the sequence of commands I ran after downloading torchlib from https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip and unzipping it in /Users/satender/pytorch/libtorch

(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

Looks like the OSX linker doesn’t support this option?
would appreciate any help here.

Other thing I tried was remove the --no-as-needed options from this place

(base) satender-mbp:example-app satender$ cat ./build/CMakeFiles/example-app.dir/link.txt
/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

removing both --as-needed and --no-as-needed references from above file and doing a make clean and make, results in the following error.

(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<long long>, c10::TensorOptions const&)", referenced from:
      torch::rand(c10::ArrayRef<long long>, c10::TensorOptions const&)::'lambda'()::operator()() const in example-app.cpp.o
  "at::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, at::Tensor const&, long long)", referenced from:
      at::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, at::Tensor const&) in example-app.cpp.o
  "c10::TensorImpl::set_autograd_meta(std::__1::unique_ptr<c10::AutogradMetaInterface, std::__1::default_delete<c10::AutogradMetaInterface> >)", 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
...

Any pointers on how to proceed?

@ssaroha did you ever find a solution for this? Getting the exact same thing. The second set of errors.