[libtorch] cannot open source file "torch/torch.h"

Timonk: <p>i am encountering a problem where in my uneducated opinion, the includes should already be included in the cmake file using</p>
<pre><code class="lang-auto">target_include_directories(testing PUBLIC "${TORCH_INCLUDE_DIRS}")
</code></pre>
<p>but cannot be opened in the .cpp file, that is in the same directory as the cmake file. here are what my files look like:</p>
<p>CMakelists.txt</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.18)

project(testing)

list(APPEND CMAKE_PREFIX_PATH "Downloads/path/to/libtorch")
find_package(Torch REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(testing test.cpp)
target_link_libraries(testing ${TORCH_LIBRARIES})
target_include_directories(testing PUBLIC "${TORCH_INCLUDE_DIRS}") #does not build without scope keyword
set_property(TARGET testing PROPERTY CXX_STANDARD 17)


if (MSVC) # I am on Visual Studio
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET testing
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $&lt;TARGET_FILE_DIR:testing&gt;)
endif (MSVC)
</code></pre>
<p>test.cpp</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main() {
	torch::Tensor tensor = torch::rand({ 2, 3 });
	std::cout &lt;&lt; tensor &lt;&lt; "\n";
}
</code></pre>
<p>in test.cpp it tells me " cannot open source file “torch/torch.h” "<br>
CppProperties.json points to the right torch folder and torch.h is present inside. it was explicitly included there as a fix suggested by visual studio, and removing it does not change the outcome</p>
<p>my question is: why would it not be able to open the file? i have tried opening Visual Studio in Administrator mode, and it did not help.<br>
i have searched the internet far and wide, including this forum, stackoverflow and reddit, but i have not found a solution to my problem so far.</p>

Timonk: <p>${TORCH_INCLUDE_DIRS} definition</p>
<pre><code class="lang-auto">if(EXISTS "${TORCH_INSTALL_PREFIX}/include")
  set(TORCH_INCLUDE_DIRS
    ${TORCH_INSTALL_PREFIX}/include
    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
else()
  set(TORCH_INCLUDE_DIRS
    ${TORCH_INSTALL_PREFIX}/include
    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
endif()
</code></pre>
<p>i found out that it 100% finds the correct config when building and /include exists (doesnt matter, it would still try), why it does not add the dependencies is beyond me, because if it did it would probably work. it not finding torch/torch.h would indicate that it does not set.</p>
<p>what i found out is that it does not find my python installation during building caffe2 dependencies, but it didnt stop building for some reason. does that matter? if yes, how can i fix that?</p>

Timonk: <p>i have found out that it was an issue with visual studio. my Project was not set up properly.<br>
What i did: i opened up a folder in my explorer and used that as my project folder. While building, cmake was not able to set up dependencies properly (it never told me that tho) because there were some critical visual studio files missing that generate when you create a project with visual studio. i tested that by making a new project with visual studio and testing my code there, and it worked.</p>