OpenGL与GLFW和GLEW-在windows下使用gcc编译
OpenGL with GLFW and GLEW - compiling with gcc on windows
我正在尝试运行一个OpenGL程序,该程序使用我自己构建的GLFW和GLEW库。我使用的启动器代码是
#include <iostream>
// GLEW
#define GLEW_STATIC
#include <glew.h>
// GLFW
#include <glfw3.h>
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", 0, 0);
glfwMakeContextCurrent(window);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
然后我键入
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32
这会产生许多"未定义的引用"错误。
代码应该是可以的,就像我以前在VisualStudio中成功运行它一样。
此命令:
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32
在Windows中是不够的。您将需要链接其他系统库。例如,Visual Studio 2012中的每个项目默认都附加了以下内容:
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib
这就是为什么它在VS中编译得很好。
CCD_ 2和CCD_。执行任何图形操作时,都需要gdi32.lib
。
第一个解决方案:
手动链接这些库:
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32 -lkernel32 -luser32 -lgdi32 -lws2_32
如果我没有记错的话,ws2_32.a
是MinGW提供的WinSock2库的名称。
第二个解决方案:
如果使用MinGW,则可以使用-mwindows
标志:
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32 -mwindows
这将链接gdi32.a
、kernel32.a
、user32.a
和ws2_32.a
等。
相关文章:
- 为什么 gcc 编译这个而 msvc 没有
- 错误:使用 clang 没有可行的重载,使用 GCC 编译
- 带有自动参数的函数使用 GCC 编译,但不使用 Visual C++ 编译
- 使用 MINGW gcc 编译时,不会为 std::string 调用重载的新运算符
- Steam 回调在 Linux / GCC 编译期间触发警告
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- 如何使用 GCC 编译指示启用选项 '-Werror'?
- 如何实现使用 gcc-4.4 编译的大向量初始化?
- 关于这个在 Linux 上使用 gcc 编译的程序中的 vtable,nm 告诉我什么?
- 使用 TDM GCC 5.1.0 编译 PDCurses36 时出错
- 为什么这段代码无法使用 gcc 4.8.5 编译,而使用 clang 编译正常
- 使用 std::addressof 时出现 GCC 7 编译错误
- 在类模板上使用 arm gcc 编译期间的隔离错误
- 使用 GCC 编译C++时如何包含 C11 标头?
- 类数据成员指针的非类型模板参数包无法使用 gcc 编译
- 是否可以使用 GCC 编译具有特定编译器标志的代码文件的一部分?
- 将 cctype 函数分配给 std::function,gcc 编译错误
- GCC 编译时出现警告:未知转义序列:"\040"
- 为什么以下代码不使用GCC编译,而是用Clang编译罚款
- GCC编译器,为较低版本的GCC编译应用程序