OpenGL C++g++编译器,无法在Windows 10(没有Visual Studio)上找到GLFW

OpenGL C++ with g++ compiler, not able to find GLFW on Windows 10 (no Visual Studio)

本文关键字:Studio Visual 没有 GLFW 编译器 C++g++ Windows OpenGL      更新时间:2023-10-16

我正在尝试使用带有g++编译器的C++从头开始设置OpenGL API。目前,我正在尝试使用 GLFW 获取一个简单的窗口系统,但是当我编译程序时,它似乎找不到 GFLW 目录。

我正在尝试从头开始执行此操作,所以我不会使用任何像Visual Studio这样的IDE,我所做的一切都是用记事本++编写的,并使用git bash的命令编写。我想这样做,我知道这是很多额外的工作,但这就是我想要实现的目标。

我的项目文件夹如下所示:

-TestProject
-main.cpp
-GFLW
-gflw3.h

在我的主文件中.cpp我已经复制粘贴了 GFLW 文档中的代码:

#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}

为了编译程序,我将 git bash 与以下命令一起使用:

g++ main.cpp -o WindowApp.exe

现在应该创建一个 exe,使我能够打开一个窗口,仅此而已。显然我做错了什么,首先我收到错误消息,找不到目录 GFLW。所以这是一个问题。

基本上,我想在这里遵循本教程ChernoProject,但不必使用Visual Studio或任何其他IDE。我知道他使用的文件与我不同,并且他做了一些叫做Linking的东西,但我不明白这如何适应我在这里的极简主义设置。

您包含相对于编译器搜索路径GLFW/glfw3.h(带尖括号),但标头位于项目的目录树中。编译器不会在项目树中搜索系统标头(包含在尖括号中),除非您通过传递参数明确告诉它这样做-Iinclude_directory。因此,在您的情况下,您需要包含如下标头:#include "GLFW/glfw3.h"(推荐),或者添加-I.参数以告诉编译器在当前目录中搜索系统标头。

同样关于链接 - 这将是您解决标题问题后的下一个问题。您的编译命令如下所示:g++ main.cpp -o WindowApp.exe并且不包含任何要链接到的库。如果您使用的是 MinGW,它必须包含以下用于静态链接到 OpenGL 和 GLFW3 的参数:-lmingw32 -lglfw3 -lopengl32 -lgdi32 -luser32

例如,我对这个项目的首选设置是:

-ProjectDirectory
-bin
-glfw3.dll (if you want to use dll)
-include
-GLFW
-glfw3.h
-glfw3native.h
-lib
-libglfw3.a
-libglfw3dll.a (if you want to use dll)
-src
-main.cpp
-build.bat

使用下面的构建内容.bat(将"Your actual path to g++"更改为系统上 g++ 的路径,或者如果它在 path 变量中,则将其设置为"g++"):

@ECHO OFF
REM Path to g++
SET G="Your actual path to g++"
SET OBJECTS=
REM Recursive every .cpp file in ./src
FOR /R "./src" %%a IN (*.cpp) DO (
CALL SET OBJECTS=%%OBJECTS%% "%%a"
)
@ECHO ON
%G% %OBJECTS% -obin/a.exe -Iinclude -Llib -lmingw32 -lglfw3 -lopengl32 -lgdi32 -luser32