Search code examples
c++compiler-errorsinclude

Not able to include a header file in C++


My code looks like this:

#include <GLFW\glfw3.h>


// define the function's prototype
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
// find the function and assign it to a function pointer
GL_GENBUFFERS glGenBuffers  = (GL_GENBUFFERS)wglGetProcAddress("glGenBuffers");
// function can now be called as normal
unsigned int buffer;
glGenBuffers(1, &buffer);

And the command I execute is this one:

g++ /home/user/git/tfg_gui/test.cpp -o /home/user/git/tfg_gui/test -I/home/user/git/tfg_gui/include

My folders look like this:

.
├── include
│   ├── glad
│   │   └── glad.h
│   ├── GLFW
│   │   ├── glfw3.h
│   │   └── glfw3native.h
│   └── KHR
│       └── khrplatform.h
├── test
└── test.cpp

The compiler error is:

/home/user/git/tfg_gui/test.cpp:1:10: fatal error: GLFW\glfw3.h: No such file or directory
    1 | #include <GLFW\glfw3.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

I think I am doing a really stupid mistake but I have already spent too much time trying to find it. Probably is that I am assuming that something is done in a way that is not the right one.

Thanks!


Solution

  • In the path for the #include you use a backward slash \:

    #include <GLFW\glfw3.h>
    

    On a POSIX system like Linux or macOS this is not a valid path-separator, instead it's considered part of the file-name.

    You need to use forward slashes / on such systems:

    #include <GLFW/glfw3.h>
    

    Since forward slashes also works on Windows, it's a good habit to always use it.