Eclipse IDE:链接 OpenGL 库 (C++)

Eclipse IDE: linking OpenGL Libraries (C++)

在数周无休止地尝试 link 一些库到 Eclipse 之后,我没有找到任何解决方案。虽然我没有收到错误,但当我按下 运行 时,会弹出一个对话框并显示 "OpenGL32Test.exe has stopped working"(顺便说一下,OpenGL32Test 是我的 Eclipse 项目的名称)。以下代码是我尝试编译的代码,但它根本不会创建它应该创建的黑色 window 和红色方块(代码取自 https://www3.ntu.edu.sg/home/ehchua/programming/opengl/HowTo_OpenGL_C.html#mingw_glut 我尝试关注的网站):

#include <windows.h>  // For MS Windows
#include <GL/glut.h>  // GLUT, includes glu.h and gl.h

/* Handler for window-repaint event. Call back when the window first appears and
   whenever the window needs to be re-painted. */
void display() {
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer

   // Draw a Red 1x1 Square centered at origin
   glBegin(GL_QUADS);              // Each set of 4 vertices form a quad
      glColor3f(1.0f, 0.0f, 0.0f); // Red
      glVertex2f(-0.5f, -0.5f);    // x, y
      glVertex2f( 0.5f, -0.5f);
      glVertex2f( 0.5f,  0.5f);
      glVertex2f(-0.5f,  0.5f);
   glEnd();

   glFlush();  // Render now
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
   glutInit(&argc, argv);                 // Initialize GLUT
   glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
   glutInitWindowSize(320, 320);   // Set the window's initial width & height
   glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
   glutDisplayFunc(display); // Register display callback handler for window re-paint
   glutMainLoop();           // Enter the infinitely event-processing loop
   return 0;
}

我用 linked 库编译程序,我去了:

Project Properties => C/C++ Builds => Settings => Tool Settings => MinGW C++ Linker

并且我添加了库文件 freeglut、glu32 和 opengl32。该程序根本不会运行。此外,在:

C/C++ General => Paths and Symbols => Libraries tab

我有 3 个库 linked。请帮我弄清楚为什么我的程序根本不会 运行!是因为我弄乱了 link 文件吗?我 运行 正在 Windows 上使用 MinGW,但我知道我正在 link 库,因为程序成功构建,控制台行似乎是正确的:

g++ -o OpenGLtest.exe main.o -lfreeglut -lglu32 -lopengl32

我将此程序所需的所有必需的 lib 文件和头文件放置在它们所属的 MinGW 根目录中 运行(include/GL 文件夹和 lib 文件夹)。有什么问题?

我找到了答案。基本上,我没有正确使用 .dll。在 windows 中,动态库是扩展名为 .dll 的库。通过将 .dll 放在与 .exe 可执行文件相同的目录中,该程序能够 运行.

  1. 所以我曾经做过同样的教程并让它工作我认为不必复制 dll....

添加“-static-libgcc -static-libstdc++”作为新项目的链接器标志。此文本应添加到链接器标志字段,可以通过在项目资源管理器中右键单击新项目并单击属性来找到该字段。在项目属性下,展开 C/C++ 构建菜单并单击设置。在 Tool Settings 选项卡下,展开 MinGW C++ Linker 菜单并单击 Miscellaneous。将文本添加到链接器标志字段,然后单击应用按钮。

当时可行,但当我今晚尝试时,效果不佳:-\

  1. 但是在实验室的说明中确实说要将 dll 复制到 MinGW bin 目录。.dll 的另一个名称是共享库...

"Download, unzip and copy header files from "include\GL" 到 "MINGW_HOME\include\GL";从 "lib" 到 "MINGW_HOME\lib" 的库和 共享library from "bin" to "MINGW_HOME\bin"(应该包含在PATH环境变量中),其中MINGW_HOME是MinGW的安装目录。"