Building a sample cpp code using static libraries

I have build static libraries on Windows 10 following this post Now I am trying to build this example code on Windows 10 with Microsoft Visual Studio 2017 using those static pytorch libraries.

CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(Pytorch_test)find_package(Torch REQUIRED)option(DOWNLOAD_MNIST “Download the MNIST dataset from the internet” ON)
message(STATUS “Downloading MNIST dataset”)
message(FATAL_ERROR “Error downloading MNIST dataset: ${DOWNLOAD_ERROR}”)
endif()add_executable(Pytorch_test C:/Users/ xxxxxx /Downloads/Pytorch_test/test_pytorch.cpp)
target_include_directories(Pytorch_test PUBLIC C:/Users/ xxxxxx /Downloads/pytorch/torch/include/torch/csrc/api/include)
target_include_directories(Pytorch_test PUBLIC C:/Users/xxxxxx/Downloads/pytorch/torch/include)
target_link_libraries(Pytorch_test PRIVATE -Wl,–whole-archive “${CMAKE_CURRENT_LIST_DIR}/*.lib”)
set_property(TARGET Pytorch_test PROPERTY CXX_STANDARD 14)if (MSVC)
add_custom_command(TARGET Pytorch_test
COMMAND ${CMAKE_COMMAND} -E copy_if_different
endif (MSVC)

Commands used:
cmake -DCMAKE_PREFIX_PATH=“C:\Users\xxxxxx\Downloads\Pytorch_test” -DCMAKE_GENERATOR_PLATFORM=x64

Can someone advise on how to build this correctly? Currently getting some LNK2019 errors while building the solution from the .sln file created after cmake.

Please rewrite this line to generate the following options for MSVC. (e.g. /WHOLEARCHIVE c10.lib /WHOLEARCHIVE torch_cuda.lib [and more]).

I edited the CMakeList.txt. I still get errors as shown below.

Do you have any suggestions on this? Thank you for your time.

Hey, I don’t think you understand what I mean. You’ll need to use /WHOLEARCHIVE otherwise the linker will try to optimize the libs away. See MSDN for more details.