Libtorch: Errors after adding #include <torch/script.h> to an existing "/CRL" project

jjesusrj: <p>I have a code in C++ to load and use a network that was design and pre-trained with Pytorch. The code runs smoothly when used independently in a C++ Visual Studio 2019 project. However, when I added the code to a pre-existing C++ windows forms program that used “/CRL”, I get a lot of errors from the Libtorch libraries such as “std” has no member “mutex” and  “E0035| <span class="hashtag">#error</span> directive: &lt;condition_variable&gt; is not supported when compiling with /clr or /clr:pure”. I tried just to include the “script.h” header to the project without any Libtorch code, but it throws all those errors too. When I remove the line “include &lt;torch/script.h&gt;” from the program it runs, but obviously without Libtorch support. Does anyone have any idea of how to fix this issue? Thank you.</p>

111443: <p>hey there I got same problem. My origin project need /crl for mscorlib to build, but it seems like libtorch don’t support to build under /crl , I had working on it for days, let me know if you got any progress.</p>

jjesusrj: <p>Hi. I am working on a quick solution where I use a TCP server program with the LibTorch code and a client program with the / crl application. It seems to work since the two programs run independently. However, since I am working with large data, transferring the data from the client to the server takes some time. Therefore, it is not suitable for my real-time processing application. Let me know if this worked for you.</p>

111443: <p>Thanks, run independently was one of my options but like you said the transferring the data between take time, I still want it in the same project, now I am trying to package my libtorh code as a dll than let my origin project include it, I hope it works. Otherwise we can only applied shared memory like socket or named pipe</p>

111443: <p>Hey recently I had made some progress with the named pipe I works quit well, I took about 2 ms to transfer, just for sharing</p>

jjesusrj: <p>Hi. Thank you for the update. In my case the TCP server works fine. However, running two programs at the same is not ideally, so I moved to QT for the user interface. QT and LibTorch work if the following is added to your header file:<br>
<span class="hashtag">#undef</span> slots<br>
<span class="hashtag">#include</span> &lt;torch/csrc/jit/serialization/import.h&gt; // required to import the model<br>
<span class="hashtag">#include</span> &lt;torch/script.h&gt;                                 // One-stop header.<br>
<span class="hashtag">#define</span> slots Q_SLOTS</p>
<p>Maybe this could help you.</p>
<p>If wrapper dll works, please let me know how to implement it.</p>

111443: <p>wrapper dll works as I tried, but I had another personal problem so I give it up, (my another program is in WIN32 buy libtorch could only be in x64)  to use dll you need to creat a dll MSCV dll solution my cmakefile is like follow:</p>
<p>cmake_minimum_required(VERSION 3.19.1 FATAL_ERROR)<br>
SET(LIBTrainer_SRC Trainer.cpp)<br>
ADD_LIBRARY(Trainer SHARED ${LIBTrainer_SRC}) <span class="hashtag">#SHARED</span> for MSCV DLL solution<br>
set(CMAKE_PREFIX_PATH “C:/Program Files/libtorch/share/cmake/Torch”)<br>
find_package(Torch REQUIRED)</p>
<p>target_link_libraries(Trainer “${TORCH_LIBRARIES}”)<br>
set_property(TARGET Trainer PROPERTY CXX_STANDARD 14)</p>
<p>if (MSVC)<br>
file(GLOB TORCH_DLLS “${TORCH_INSTALL_PREFIX}/lib/*.dll”)<br>
add_custom_command(TARGET Trainer<br>
POST_BUILD<br>
COMMAND ${CMAKE_COMMAND} -E copy_if_different<br>
${TORCH_DLLS}<br>
$&lt;TARGET_FILE_DIR:Trainer&gt;)<br>
endif (MSVC)</p>
<p>Then you can create a dll solution you can search for MSCV dll solution you for following</p>