clang in neovim giving pp_file_not_found error for c++/pytorch basic example

7.7k Views Asked by At

I'm following this very basic c++/pytorch example:

pytorch_installing

And I can walk through this example with no errors. However, when creating the example-app.cpp file (or editing it at any point in time) with neovim, clang throws an error 'torch/torch.h' file not found [clang: pp_file_not_found]. My CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
include_directories(SYSTEM /home/username/Downloads/libtorch/)
set(CMAKE_PREFIX_PATH "/home/username/Downloads/libtorch/share/cmake/Torch")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

This isn't a big issue for me with this small of a project. But I will be working on a larger project with libtorch and would like clang to recognize <torch/torch.h>. There were a couple similar Stackoverflow questions, but no answers.

Update:

I believe this is happening because clang is not seeing torch/torch.h because it is not part of the include paths. I printed the include paths for clang and it was not list. So I tried adding the include paths of libtorch to /usr/include/ but then it has issues seeing other header files referenced by header files I added. So I added the libtorch/include/torch/csrc/api/include/torch/ directory to /usr/include so that it can read #include <torch/torch.h. But then other header files are referenced in those header files with specified directory paths outside of the libtorch/include/torch/csrc/api/include/torch/ path.

For example, libtorch/include/torch/csrc/ contains WindowsTorchApiMacro.h. There are other paths within the libtorch directory that contain more header files. I tried to add all header files to \usr\include but still received an error. I'm sure the package as a whole needs to be used. The original cmake file looks like this:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

So cmake is able to find and reference all of those header files properly, and I know you can compile it at the command line with clang, but how can clang within neovim (during editing) see this package without error? I thought about using a package manager like vcpkg, but they do not have libtorch available, unfortunately. I can include output from a verbose and successful cmake run if needed.

2

There are 2 best solutions below

0
On BEST ANSWER

Since the compilation succeeds with cmake-make, it is possible to ask CMake to generate the compilation database (typically a compile_commands.json file) with cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1.

Once the file is available, it can be read by the autocompletion compiler (clang in this case), which prevents the autocompleter from being lost in the filesystem, because all flags and headers are specified for all compilation units. The compilation database just needs to be put where the autocompletion plugin expects it to be.

0
On

You can add at the beginning of your CMakeList.txt

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Or as a cache variable in a CMakePresets.json file if you use one :

{
    "version": 1,
    "configurePresets": [
        {
            "name": "debug",
            "displayName": "Debug",
            "binaryDir": "${sourceDir}/build",
            "generator": "Unix Makefiles",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
                "CMAKE_CXX_FLAGS_INIT": "-std=c++17 -g -Wall -Wextra -Wpedantic -Werror -Weffc++  -Wshadow -O0"
            }
        },
        {
            "name": "release",
            "displayName": "Release",
            "binaryDir": "${sourceDir}/build",
            "generator": "Unix Makefiles",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_CXX_FLAGS_INIT": "-std=c++17 -march=haswell -flto -O3"
            }
        }
    ]
}