I have a simple project with use of libtorch. The structure of the project is the following:
MyLibtorchProject/
├── CMakeLists.txt
├── main.cpp
├── libtorch/
└── build/
The content of CMakeLists.txt is:
cmake_minimum_required(VERSION 3.10)
project(MyLibtorchProject)
# Set the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Set the LibTorch directory path
set(Torch_DIR "C:/Desktop/Audio_C++_JUCE/MyLibtorchProject/libtorch/share/cmake/Torch")
# Find the LibTorch package
find_package(Torch REQUIRED)
# Add the executable target
add_executable(MyLibtorchProject main.cpp)
# Link LibTorch to your executable
target_link_libraries(MyLibtorchProject "${TORCH_LIBRARIES}")
# Ensure the target uses C++17 features
target_compile_features(MyLibtorchProject PRIVATE cxx_std_17)
# Set the C++ flags required by LibTorch
set_property(TARGET MyLibtorchProject PROPERTY CXX_STANDARD 14)
set_property(TARGET MyLibtorchProject PROPERTY CXX_STANDARD_REQUIRED ON)
# Set debug and release configurations if needed
if (MSVC)
# Enable multithreading and increase the number of sections in object files
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /bigobj /std:c++17")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -std=c++17")
endif()
And, the main.cpp is:
#include <torch/torch.h>
#include <iostream>
int main() {
// Create a simple tensor
torch::Tensor tensor = torch::rand({2, 3});
std::cout << "Tensor:\n" << tensor << std::endl;
return 0;
}
I have run cmake as followed
cmake -DCMAKE_PREFIX_PATH="C:/Desktop/Audio_C++_JUCE/MyLibtorchProject/libtorch" .. -DCMAKE_BUILD_TYPE=Release ..
then, I have built the MyLibtorchProject project with success. But when I run it, I encountered the following:
The procedure entry point
?at_Half_metadata_index@detail@caffe2@@3GB could not be located in the dynamic link library
C:\Desktop\Audio_C++_JUCE\MyLibtorchProject\build\Debug\MyLibtorchProject.exe
I have introduced in the PATH Sytem C:\Desktop\Audio_C++_JUCE\MyLibtorchProject\libtorch
So, what am I missing?
/std:c++17and-std=c++17flags are not necessary since you have already asked CMake for C++17 twice at that point.