vscode-clang-tidy icon indicating copy to clipboard operation
vscode-clang-tidy copied to clipboard

'ros/ros.h' file not found clang-tidy(clang-diagnostic-error)

Open shac12 opened this issue 3 years ago • 7 comments

When working in a ROS Catkin workspace, intellisense is able to find all my header files, yet clang complains that it cannot find them. It does not complain about std headers such as vector or sstream but it cannot find ros specific header files.

My cpp properties are:

{
  "configurations": [
    {
      "browse": {
        "databaseFilename": "",
        "limitSymbolsToIncludedHeaders": true
      },
      "includePath": [
        "/home/ftb/catkin_ws/devel/include/**",
        "/opt/ros/melodic/include/**",
        "/usr/include/**"
      ],
      "name": "ROS"
    }
  ],
  "version": 4
}

My settings.json is:

{
    "python.autoComplete.extraPaths": [
        "/home/ftb/catkin_ws/devel/lib/python2.7/dist-packages",
        "/opt/ros/melodic/lib/python2.7/dist-packages"
    ],
    "python.pythonPath": "/usr/bin/python",
    "files.associations": {
        "vector": "cpp"
    }
}

My user settings are as follows:

{
    "python.showStartPage": false,
    "python.languageServer": "Microsoft",
    "workbench.editorAssociations": [
        {
            "viewType": "jupyter.notebook.ipynb",
            "filenamePattern": "*.ipynb"
        }
    ],
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "[cpp]": {
        "editor.defaultFormatter": "ms-vscode.cpptools"
    },
    "clang-tidy.fixOnSave": true,
    "[python]": {
        "editor.defaultFormatter": "ms-python.python"
    },
    "workbench.iconTheme": "vscode-icons",
    "clang-tidy.compilerArgs": [ "-p /home/ftb/catkin_ws/build"
    
    ],
    "clang-tidy.compilerArgsBefore": [
        "-p  /home/ftb/catkin_ws/build"
    ]
}

shac12 avatar Jun 09 '21 01:06 shac12

Are you using a compile_commands.json file?

notskm avatar Jun 09 '21 17:06 notskm

Are you using a compile_commands.json file?

Hi thanks for replying so quickly.

I don't think so. Nothing in the ros documentation talks about a compile commands file but it's possible that this has been abstracted away from us by the Catkin tool.

Also it should be noted that everything compiles with Catkin build normally with no errors.

shac12 avatar Jun 09 '21 20:06 shac12

You need to either manually tell clang-tidy where to find your headers or have your build system generate a compile_commands.json file.

I'm not sure what build system you're using, but I know CMake can generate a compile_commands.json file. Just pass -DCMAKE_EXPORT_COMPILE_COMMANDS=ON on the command line. Alternatively, the cmake-tools extension has a setting for that. If you're not using CMake or another tool that can generate this file for you, you'll have to list your includes manually.

According to the clang-tidy docs, it looks like you have to pass the include paths to clang itself. The provide the following example: clang-tidy test.cpp -- -Imy_project/include -DMY_DEFINES ... https://clang.llvm.org/extra/clang-tidy/

I think you should be able to do this using the clang-tidy.compilerArgs setting.

notskm avatar Jun 09 '21 21:06 notskm

Thank you so much for the help.

The way that our project is structured is that there are a number of cmake packages that all depend on ROS. Catkin goes through each of these packages and builds them using cmake and their cmake lists. Is there a way to programmatically set clang to look for these ROS headers for all of these packages ? For example, An environment variable I can tweak to get clang to look in the right places ?

I will try what you have mentioned shortly when I get to work and I'll be sure to post back with the results.

shac12 avatar Jun 09 '21 23:06 shac12

Is there a way to programmatically set clang to look for these ROS headers for all of these packages? For example, An environment variable I can tweak to get clang to look in the right places?

Other than PATH? I have no idea. This is just a plugin that integrates clang-tidy into vscode. You'll have to look at the clang docs for that.

notskm avatar Jun 10 '21 06:06 notskm

I see this too even with a compile_commands.json file (which the ms cpp plugin picks up fine).

kjeremy avatar Aug 18 '21 22:08 kjeremy

Not 100% ideal, but I solved this by setting clang-tidy.buildPath to the compile_commands.json folder (variables like ${workspaceFolder} don't seem to work, but that's somewhat expected):

vscode-clang-tidy

A good way to enable CMAKE_EXPORT_COMPILE_COMMANDS by default is to put it into your configurationPresets's cacheVariables property in your project's CMakePresets.json. In a pinch you can also change the setting temporarily (until the next clean build) in your CMakeCache.txt.

Thanks so much for this great extension! :smiley:

mvf avatar May 12 '22 10:05 mvf