Hayk
Hayk

Reputation: 207

Why I get Error "No such file or directory", when I try to include a header file?

I have main.cpp and add.cpp add.h files

When I try to include add.h I get following compilation error

main.cpp:2:10: fatal error: add.h: No such file or directory
    2 | #include <add.h>
      |          ^~~~~~~
compilation terminated.

Here is the code:

main.cpp

#include <iostream>
#include <add.h>
using namespace std;

int main()
{
    cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
    return 0;
} 

add.cpp

int add(int x, int y)
{
    return x + y;
};

add.h

int add(int x, int y);

Upvotes: 2

Views: 8199

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58805

#include <add.h> causes the compiler to search the system include directories for the file add.h, but not the current directory. If you want to include a file add.h which is in the current directory, use:

#include "add.h"

Upvotes: 5

Related Questions