2

in a folder I got the following files:

book.h, book.cpp , card.h, card.cpp

these are the content of the book.h

class book {

public:
        string title;
        string author;
        string get_title();
        string get_author();

}

book::book(string title, string author){
   title = title;
   author = author;
}

this is my book.cpp:

#include <book.h>
#include <iostream>
#include <string>

using namespace std;

string book::get_title(){

  return title;
}

string book::get_author(){
 return author;
}

int main(){

cout << ¨it works! \n¨ 
}

I I try to compile using g++ -c book.cpp, I keep getting an error message saying:

book.cpp:1:18: fatal error: book.h: No such file or directory compilation terminated.

Suraj Jain
  • 4,463
  • 28
  • 39
Tito
  • 109
  • 5
  • 13

2 Answers2

2

The difference between

#include <book.h>

and

#include "book.h"

is that the first one looks for the file book.h in INCLUDE path only. The latter looks for the file first in the directory where the source file is and if it was not found then it looks for it in INCLUDE path. So you can resolve your issue either by replacing angle brackets with quotes, or by adding your directory to INCLUDE. You can do it by compiling like this

g++ -I . -c book.cpp
Alan Milton
  • 374
  • 4
  • 13
0

#include statements use <> brackets when the included file is part of your "Include" path, typically (though not exclusively) when the file is coming from a library, or the C++ Standard Library.

"" is used instead when the file is local to the project and local to the source code.

If you mix them up, you're going to have a bad time.

Xirema
  • 19,889
  • 4
  • 32
  • 68