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

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| #error directive: <condition_variable> 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 <torch/script.h>” from the program it runs, but obviously without Libtorch support. Does anyone have any idea of how to fix this issue? Thank you.

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.

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.

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

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

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:
#undef slots
#include <torch/csrc/jit/serialization/import.h> // required to import the model
#include <torch/script.h> // One-stop header.
#define slots Q_SLOTS

Maybe this could help you.

If wrapper dll works, please let me know how to implement it.

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:

cmake_minimum_required(VERSION 3.19.1 FATAL_ERROR)
SET(LIBTrainer_SRC Trainer.cpp)
ADD_LIBRARY(Trainer SHARED ${LIBTrainer_SRC}) #SHARED for MSCV DLL solution
set(CMAKE_PREFIX_PATH “C:/Program Files/libtorch/share/cmake/Torch”)
find_package(Torch REQUIRED)

target_link_libraries(Trainer “${TORCH_LIBRARIES}”)
set_property(TARGET Trainer PROPERTY CXX_STANDARD 14)

if (MSVC)
file(GLOB TORCH_DLLS “${TORCH_INSTALL_PREFIX}/lib/*.dll”)
add_custom_command(TARGET Trainer
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:Trainer>)
endif (MSVC)

Then you can create a dll solution you can search for MSCV dll solution you for following