Linker Error MacOSX lib torch

I’m trying to compile a C++ program that essentially runs a simulation for a metal lattice, and requires the evaluation of a machine learning model that I’ve trained on pytorch. I’m attempting to load the pre-trained model, and have had success in downloading the lib torch library. My CmakeLists.txt file is below:

cmake_minimum_required(VERSION 3.16)


set(Torch_DIR /Users/quinnhollister/CLionProjects/Langevin/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)


add_executable(PytorchTest main.cpp)
    target_link_libraries(PytorchTest OpenMP::OpenMP_CXX)
target_link_libraries(PytorchTest "${TORCH_LIBRARIES}")

The strange thing is that when I run:
$ Cmake …
everything works, no issue. But when I end up trying to actually compile the program:
$ make
*I run into issues with the linker. My error message is below:

[ 50%] Building CXX object CMakeFiles/PytorchTest.dir/main.cpp.o
[100%] Linking CXX executable PytorchTest
ld: unknown option: --no-as-needed
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [PytorchTest] Error 1
make[1]: *** [CMakeFiles/PytorchTest.dir/all] Error 2
make: *** [all] Error 2

I’m running MacOSX, and using the compilers cc and c++.
I can see that the error has to do with the linker not knowing that specific flag “–no-as-needed”, but I have no idea how to suppress that flag, or if it hints at a larger issue. Any help would be appreciated.


Can you create an issue at
Add ‘build’ tag on it, someone there will take a look at.

Ensure that you downloaded the correct libtorch library for the correct platform. I faced this problem because I downloaded the library meant for Linux (should be macOS).

1 Like