notskm / vscode-clang-tidy

MIT License
49 stars 25 forks source link

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

Open shac12 opened 3 years ago

shac12 commented 3 years ago

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"
    ]
}
notskm commented 3 years ago

Are you using a compile_commands.json file?

shac12 commented 3 years ago

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.

notskm commented 3 years ago

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.

shac12 commented 3 years ago

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.

notskm commented 3 years ago

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.

kjeremy commented 3 years ago

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

mvf commented 2 years ago

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: