-2

I'm trying to code a project in C++ using Visual Studio Code on Ubuntu 22.04. I need to include a header file that is located in a folder full of other header files and sub-directories (main folder name "depends"). I wrote #include "polar.h" Visual Studio Code find the header file but it suggested me to add the path in the Include Path on the configuration window of the C/C++ Extension. I add ${workspaceFolder}/depends/polar-include but the compiler still says No such file or directory.

The weird fact is that all the classes and functions from the missing header file are not giving me any errors. I've already tried to using the full path in the #include tag (so something like #include "depends/include/polar.h" and the compiler found the file but it gave me a missing file error on a header file linked to the original one. I've already done this project using QtCreator so I import the Include Path from Qt to VSC but nothing changed. This is my Include Path:

"includePath": [
    "${workspaceFolder}/**",
    "${workspaceFolder}",
    "${workspaceFolder}/depends/include/polar-include/**",
    "${workspaceFolder}/depends/include/polar-include/modules/**",
    "${workspaceFolder}/depends/include/polar-include/modules/stress/**",
    "${workspaceFolder}/depends/include/polar-include/b2bInterface/**",
    "${workspaceFolder}/depends/include/**"
    ],

I don't understand why including all the paths as I did in QtCreator doesn't work, as if VSC can't link all the subdirectories and files.

Edit: this is task.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

As suggested in the comments, I edited the task.json by adding:

"-I${workspaceFolder}",
                "-I${workspaceFolder}/depends/include",
                "-I${workspaceFolder}/depends/include/polar-include",
                "-I${workspaceFolder}/depends/include/polar-include/b2bInterface",
                "-I${workspaceFolder}/depends/include/polar-include/modules",
                "-I${workspaceFolder}/depends/include/polar-include/modules/stress",
                "-L${workspaceFolder}/depends/lib/Linux_x86_64",
                "-lusb-1.0",
                "-lprotobuf",
                "-lb2bInterfaceCpp",
                "-lb2bInterface"

Now it works.

dod0bob
  • 1
  • 1
  • It would be helpful to post full content of your ```.vscode/tasks.json``` file. – vtm11 Aug 29 '23 at 11:00
  • I do it right now. – dod0bob Aug 29 '23 at 11:17
  • I suggest you add ```"-I${workspaceFolder}/../.."``` (modify path accordingly to your project structure) to the ```"args"``` section of your ```tasks.json```. – vtm11 Aug 29 '23 at 11:18
  • ```"includePath"``` is for IntelliSense only, it does not communicate anything to compiler. – vtm11 Aug 29 '23 at 11:19
  • @vtm11 do I need to add all paths in `"args"` section or just `"-I${workspaceFolder}/../.."`? Thanks – dod0bob Aug 29 '23 at 11:32
  • From your post it looks like ```-I${workspaceFolder}/**``` should be enough. – vtm11 Aug 29 '23 at 11:40
  • EDIT: Since gcc / g++ does not support recursive include folders search you need to add all paths to your header files. Like ```"-I${workspaceFolder}/depends/include"``` and so on. Another option is to use some [shell script](https://stackoverflow.com/a/59621930/16648033) to look for headers in nested folders. – vtm11 Aug 29 '23 at 11:57
  • Ok, I'll try to add all of the manually. – dod0bob Aug 29 '23 at 12:01
  • @vtm11 I add some more info due to new error after the original one, would you mind to have a look? – dod0bob Aug 29 '23 at 13:03
  • 1
    *undefined reference to* is [linker error](https://stackoverflow.com/a/19662783), it means that while linking your app implementation for corresponding functions is missing. If ```heart::heart()``` and ```B2bInterface::initCommunication()``` are part of your project make sure that source files containing implementation are included in ```tasks.json```. For now your ```"args"``` section contains ```main.cpp``` only. If these functions are from some external library make sure that this library is installed properly in your environment. – vtm11 Aug 29 '23 at 14:24
  • `"${file}",` is the bug. It says build only the active file. The documentation tells you about this default behavior and how to adjust your tasks.json to build all files instead of just the active one: [https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson) there are many duplicates for this problem since many new people experience this early in their VSCode usage. – drescherjm Aug 29 '23 at 14:33
  • An [example](https://stackoverflow.com/a/63539447/16648033) how to compile multiple *.cpp files in single directory. Your case can be more complicated if your source files reside in many different folders. – vtm11 Aug 29 '23 at 14:37
  • I found a solution, I'm editing the post with the correct task.json I use. Thanks to everyone! – dod0bob Aug 29 '23 at 14:46
  • ***I'm editing the post with the correct task.json I use.*** You probably should just agree to the duplicate. – drescherjm Aug 29 '23 at 14:47

0 Answers0