My workspace is :
├── README.md
└── sim
├── Makefile
├── include
│ ├── add.cpp
│ └── add.h
│
└── src
└── main.cpp
My main.cpp is :
// main.cpp
#include <iostream>
#include "Add.h"
using namespace std;
int main()
{
int a = 10;
int b = 20;
int c;
c = add(a, b);
cout << a << " + " << b << " = " << c << endl;
return 0;
}
My add.h is:
// Add.h
#include <iostream>
#include "main.cpp"
using namespace std;
int add(int a, int b);
My add.cpp is:
//add.cpp
#include <iostream>
#include "Add.h"
using namespace std;
int add(int a, int b)
{
return a + b;
}
My c_cpp_properties.json is:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/sim/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
My launch.json is:
{
"version": "0.2.0",
"configurations": [
{
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /bin/gdb -q --interpreter=mi",
"name": "C/C++",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "compile",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
My tasks.json is:
{
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I${workspaceFolder}/sim/include/**"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
And I encountered an error when I pressed F5 in main.cpp :
* Executing task: g++ -g /home/chris/Code/graduation-project/sim/src/main.cpp -o /home/chris/Code/graduation-project/sim/src/main -I/home/chris/Code/graduation-project/sim/include/**
/home/chris/Code/graduation-project/sim/src/main.cpp:3:10: fatal error: Add.h: No such file or directory
3 | #include "Add.h"
| ^~~~~~~
compilation terminated.
* The terminal process "g++ '-g', '/home/chris/Code/graduation-project/sim/src/main.cpp', '-o', '/home/chris/Code/graduation-project/sim/src/main', '-I/home/chris/Code/graduation-project/sim/include/**'" failed to launch (exit code: 1).
* Terminal will be reused by tasks, press any key to close it.
What should I do ?
How can I get my main.cpp and its header files to work together when they are in different folders? I am using Ubuntu 22.04 under WSL2 and connecting remotely via VSCode.
I had a similar problem as is described by the first question. Based on the discussion here I was able to get mine working.
What I find disconcerting (and confusing) is that the Intellisense Include path is maintained separately from the compiler's Include path. The Include path in the c_cpp_properties.json seems to only affect Intellisense, whereas the compiler option -I is maintained in the tasks.json; the GUI interface doesn't automatically keep the two coordinated. (Unless mine is broken.) Also, there seems to be a difference in convention, where the /** works to recursively specify the include directory for the Intellisense, /* shuts off the recursion for the compiler option. Or, at least that is what it appears for me.