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)
project(PytorchTest)

set(CMAKE_CXX_STANDARD 14)

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

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")

add_executable(PytorchTest main.cpp)
if(OpenMP_CXX_FOUND)
    target_link_libraries(PytorchTest OpenMP::OpenMP_CXX)
endif()
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.

Thanks!

@qhollist
Can you create an issue at https://github.com/pytorch/pytorch/issues
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).

2 Likes