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

时间:2015-03-27 22:34:55

标签: c++ eclipse windows opengl libraries

经过数周无休止地尝试将一些库链接到Eclipse之后,我找不到任何解决方案。虽然我没有收到错误,但当我按下运行时,会弹出一个对话框并说“OpenGL32Test.exe已停止工作”(顺便说一下,OpenGL32Test是我的Eclipse项目的名称)。下面的代码是我尝试编译的,但它根本不会创建它应该的黑色窗口和红色方块(代码来自我试图遵循的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;
}

我使用链接库来编译程序,我去了:

  

项目属性=&gt; C / C ++ Builds =&gt;设置=&gt;工具设置=&gt;   MinGW C ++链接器

我添加了库文件freeglut,glu32和opengl32。该程序根本无法运行。另外,在:

  

C / C ++ General =&gt;路径和符号=&gt; “库”选项卡

我有3个图书馆链接。请帮我弄清楚为什么我的程序根本不会运行!是因为我弄乱了链接文件吗?我在Windows上运行并使用MinGW,但我知道我正在链接库,因为程序成功构建,并且控制台线看起来是正确的:

  

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

  1. 所以我曾经做过同样的教程并让它工作我认为不必复制dll ....
  2. 将“-static-libgcc -static-libstdc ++”添加为新项目的链接器标志。此文本应添加到链接器标志字段,可以通过在项目浏览器中右键单击新项目并单击属性来找到该字段。在Project Properties下,展开C / C ++ Build菜单,然后单击Settings。在“工具设置”选项卡下,展开“MinGW C ++链接器”菜单,然后单击“其他”。将文本添加到“链接器标志”字段,然后单击“应用”按钮。

    那当时有效,但是当我今晚尝试它时,效果并不好: - \

    1. 但在实验室的说明中,它说将dll复制到MinGW bin目录.. .dll的另一个名称是共享库...
    2. “从”include \ GL“下载,解压缩并复制头文件到”MINGW_HOME \ include \ GL“;从”lib“到”MINGW_HOME \ lib“的库和 共享库来自” bin“to”MINGW_HOME \ bin“ (应该包含在PATH环境变量中),其中MINGW_HOME是MinGW安装目录。”