Building a sample cpp code using static libraries

Ann: <p>I have build static libraries on Windows 10 following this post <a href="https://slack-redir.net/link?url=https%3A%2F%2Fdiscuss.pytorch.org%2Ft%2Flibtorch-static-library%2F73178%2F9" rel="nofollow noopener">https://discuss.pytorch.org/t/libtorch-static-library/73178/9</a>. Now I am trying to build this example code <a href="https://slack-redir.net/link?url=https%3A%2F%2Fgithub.com%2Fpytorch%2Fexamples%2Fblob%2Fmaster%2Fcpp%2Fdcgan%2Fdcgan.cpp" rel="nofollow noopener">https://github.com/pytorch/examples/blob/master/cpp/dcgan/dcgan.cpp</a>  on Windows 10 with Microsoft Visual Studio 2017 using those static pytorch libraries.</p>
<p><strong>CMakeLists.txt</strong> is as follows:</p>
<p>cmake_minimum_required(VERSION 3.0 FATAL_ERROR)<br>
project(Pytorch_test)find_package(Torch REQUIRED)option(DOWNLOAD_MNIST “Download the MNIST dataset from the internet” ON)<br>
if (DOWNLOAD_MNIST)<br>
message(STATUS “Downloading MNIST dataset”)<br>
execute_process(<br>
COMMAND python ${CMAKE_CURRENT_LIST_DIR}/…/tools/download_mnist.py<br>
-d ${CMAKE_BINARY_DIR}/data<br>
ERROR_VARIABLE DOWNLOAD_ERROR)<br>
if (DOWNLOAD_ERROR)<br>
message(FATAL_ERROR “Error downloading MNIST dataset: ${DOWNLOAD_ERROR}”)<br>
endif()<br>
endif()add_executable(Pytorch_test C:/Users/ xxxxxx /Downloads/Pytorch_test/test_pytorch.cpp)<br>
target_include_directories(Pytorch_test PUBLIC C:/Users/ xxxxxx /Downloads/pytorch/torch/include/torch/csrc/api/include)<br>
target_include_directories(Pytorch_test PUBLIC C:/Users/xxxxxx/Downloads/pytorch/torch/include)<br>
target_link_libraries(Pytorch_test PRIVATE -Wl,–whole-archive “${CMAKE_CURRENT_LIST_DIR}/*.lib”)<br>
set_property(TARGET Pytorch_test PROPERTY CXX_STANDARD 14)if (MSVC)<br>
add_custom_command(TARGET Pytorch_test<br>
POST_BUILD<br>
COMMAND ${CMAKE_COMMAND} -E copy_if_different<br>
$&lt;TARGET_FILE_DIR:Pytorch_test&gt;)<br>
endif (MSVC)</p>
<p><strong>Commands used:</strong><br>
cmake -DCMAKE_PREFIX_PATH=“C:\Users\xxxxxx\Downloads\Pytorch_test” -DCMAKE_GENERATOR_PLATFORM=x64</p>
<p>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.</p>

peterjc123: <aside class="quote no-group" data-username="Ann" data-post="1" data-topic="89222">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/76d3ee/48.png" class="avatar"> Ann:</div>
<blockquote>
<p>target_link_libraries(Pytorch_test PRIVATE -Wl,–whole-archive “${CMAKE_CURRENT_LIST_DIR}/*.lib”)</p>
</blockquote>
</aside>
<p>Please rewrite this line to generate the following options for MSVC. (e.g. <code>/WHOLEARCHIVE c10.lib /WHOLEARCHIVE torch_cuda.lib [and more]</code>).</p>

Ann: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/8/48bf74d7547fbccbb97b2456beb707206fc0d749.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/48bf74d7547fbccbb97b2456beb707206fc0d749" title="CMakeList_post"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/8/48bf74d7547fbccbb97b2456beb707206fc0d749_2_690x150.jpeg" alt="CMakeList_post" data-base62-sha1="anyzqepV6dNB1PWhvs2qjmAgHk5" width="690" height="150" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/8/48bf74d7547fbccbb97b2456beb707206fc0d749_2_690x150.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/8/48bf74d7547fbccbb97b2456beb707206fc0d749_2_1035x225.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/8/48bf74d7547fbccbb97b2456beb707206fc0d749_2_1380x300.jpeg 2x" data-dominant-color="F3F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">CMakeList_post</span><span class="informations">1756×384 189 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I edited the CMakeList.txt. I still get errors as shown below.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0628605f40ba64f8375c8acf97b92a1dfa7b252.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b0628605f40ba64f8375c8acf97b92a1dfa7b252" title="MSVC_Error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0628605f40ba64f8375c8acf97b92a1dfa7b252_2_690x198.jpeg" alt="MSVC_Error" data-base62-sha1="pan7t8WCUz7hfkTzKRLWZTw9xE6" width="690" height="198" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0628605f40ba64f8375c8acf97b92a1dfa7b252_2_690x198.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0628605f40ba64f8375c8acf97b92a1dfa7b252_2_1035x297.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0628605f40ba64f8375c8acf97b92a1dfa7b252.jpeg 2x" data-dominant-color="363638"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">MSVC_Error</span><span class="informations">1037×298 108 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Ann: <p><a class="mention" href="/u/peterjc123">@peterjc123</a><br>
Do you have any suggestions on this? Thank you for your time.</p>

peterjc123: <p>Hey, I don’t think you understand what I mean. You’ll need to use <code>/WHOLEARCHIVE</code> otherwise the linker will try to optimize the libs away. See <a href="https://docs.microsoft.com/en-us/cpp/build/reference/wholearchive-include-all-library-object-files?view=vs-2019" rel="nofollow noopener">MSDN</a> for more details.</p>