1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I did what you said but still getting the same error. Then I tried copying the Random1.h file to the mains directory where the cpp file resides. This then worked. Now changing to #include <Random.h> gives me the same error even when it resides in directory of the cpp. So I am not able to follow the line of reasoning. Please elaborate and thanks for helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.