Building a sample cpp code using static libraries

I have build static libraries on Windows 10 following this post https://discuss.pytorch.org/t/libtorch-static-library/73178/9. Now I am trying to build this example code https://github.com/pytorch/examples/blob/master/cpp/dcgan/dcgan.cpp 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)
if (DOWNLOAD_MNIST)
message(STATUS “Downloading MNIST dataset”)
execute_process(
COMMAND python ${CMAKE_CURRENT_LIST_DIR}/…/tools/download_mnist.py
-d ${CMAKE_BINARY_DIR}/data
ERROR_VARIABLE DOWNLOAD_ERROR)
if (DOWNLOAD_ERROR)
message(FATAL_ERROR “Error downloading MNIST dataset: ${DOWNLOAD_ERROR}”)
endif()
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
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE_DIR:Pytorch_test>)
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.

@peterjc123
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.