无法在 C++ 中包含头文件
Not able to include a header file in C++
我的代码如下所示:
#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);
而我执行的命令是这个:
g++ /home/user/git/tfg_gui/test.cpp -o /home/user/git/tfg_gui/test -I/home/user/git/tfg_gui/include
我的文件夹如下所示:
.
├── include
│ ├── glad
│ │ └── glad.h
│ ├── GLFW
│ │ ├── glfw3.h
│ │ └── glfw3native.h
│ └── KHR
│ └── khrplatform.h
├── test
└── test.cpp
编译错误为:
/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.
我想我犯了一个非常愚蠢的错误,但我已经花了太多时间试图找到它。可能是我假设某件事是以不正确的方式完成的。
谢谢!
在 #include
的路径中使用反斜杠 \
:
#include <GLFW\glfw3.h>
在像 Linux 或 macOS 这样的 POSIX 系统上,这不是有效的 path-separator,而是被认为是 file-name.
的一部分
您需要在此类系统上使用正斜杠 /
:
#include <GLFW/glfw3.h>
由于正斜杠也适用于 Windows,因此始终使用它是一个好习惯。
您正在使用反斜杠,请将其转换为正斜杠。
#include <GLFW/glfw3.h>
反斜杠是转义字符,它们用于将键盘上始终可用的常规字母(n
、a
等)转换为您既无法阅读也无法阅读的特殊字符参见(LF
、Beep
等)。
在这种情况下,编译器将文件名视为 GLFW[unkown-character]lfw3.h
,但该文件不存在。这就是错误的来源。
我的代码如下所示:
#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);
而我执行的命令是这个:
g++ /home/user/git/tfg_gui/test.cpp -o /home/user/git/tfg_gui/test -I/home/user/git/tfg_gui/include
我的文件夹如下所示:
.
├── include
│ ├── glad
│ │ └── glad.h
│ ├── GLFW
│ │ ├── glfw3.h
│ │ └── glfw3native.h
│ └── KHR
│ └── khrplatform.h
├── test
└── test.cpp
编译错误为:
/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.
我想我犯了一个非常愚蠢的错误,但我已经花了太多时间试图找到它。可能是我假设某件事是以不正确的方式完成的。
谢谢!
在 #include
的路径中使用反斜杠 \
:
#include <GLFW\glfw3.h>
在像 Linux 或 macOS 这样的 POSIX 系统上,这不是有效的 path-separator,而是被认为是 file-name.
的一部分您需要在此类系统上使用正斜杠 /
:
#include <GLFW/glfw3.h>
由于正斜杠也适用于 Windows,因此始终使用它是一个好习惯。
您正在使用反斜杠,请将其转换为正斜杠。
#include <GLFW/glfw3.h>
反斜杠是转义字符,它们用于将键盘上始终可用的常规字母(n
、a
等)转换为您既无法阅读也无法阅读的特殊字符参见(LF
、Beep
等)。
在这种情况下,编译器将文件名视为 GLFW[unkown-character]lfw3.h
,但该文件不存在。这就是错误的来源。