Search code examples
c++ubuntuvisual-studio-codeheader-filesinclude-path

Visual Studio C++ program cannot find the include folder even when specified


I am just starting out in C/C++ programming using Visual Studio Code installed. I have all the necessary packages installed. My CPP project has the following directory structure:

DesignCPP #(parent/working directory)
/include
/mains
/source

The c_cpp_properties.json file has the following code:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/home/pinaki/Finance_with_C++/DesignCPP/include",
                "${workspaceRoot}"                
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

Snippet of the C++ Program I am trying to run:

#include <Random1.h>
#include <iostream>
#include <cmath>
using namespace std;

double SimpleMonteCarlo1(double Expiry,
                     double Strike,
                     double Spot,
                     double Vol,
                     double r,
                     unsigned long NumberOfPaths)
{
...
..

Ctrl+Shift+B returns:

> Executing task:  g++ -g mains/SimpleMCMain1.cpp -o SimpleMCMain1.out && clear && ./SimpleMCMain1.out <

mains/SimpleMCMain1.cpp:8:10: fatal error: Random1.h: No such file or directory
 #include <Random1.h>
          ^~~~~~~~~~~
compilation terminated.
The terminal process terminated with exit code: 1

As you can see it is not able to locate the header files in the include directory which has been specified explicitly in the cpp_properties.json file.

I read a lot similar stack question question to figure out the problem without any success.

Please advice since I am struggling with it since yesterday.


Solution

  • That's because you've written #include <someFile.h>. What you should've done is use #include "someFile.h".

    When using <> you tell the compiler to search its own directory for includes. When using "" you are telling the compiler to search the specified include path.

    Edited for clarity: Simply specify the -I flag for gcc with your include folder. For you it would be something like this g++ -I include -g mains/SimpleMCMain1.cpp -o SimpleMCMain1.out && clear && ./SimpleMCMain1.out