GLUT程序链接错误 [英] GLUT program link error

查看:127
本文介绍了GLUT程序链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编译一个GLUT程序

I want to compile a GLUT program

#include <GL/glut.h>

int main(int argc, char **argv) {

    // init GLUT and create Window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D- GLUT Tutorial");

    return 1;

}

编译和链接命令:

gcc -o main.exe main.c -lglut32

结果:

main.o:main.c:(.text.startup+0x1c): undefined reference to `glutInit'
main.o:main.c:(.text.startup+0x28): undefined reference to `glutInitDisplayMode'
main.o:main.c:(.text.startup+0x3c): undefined reference to `glutInitWindowPosition'
main.o:main.c:(.text.startup+0x50): undefined reference to `glutInitWindowSize'
main.o:main.c:(.text.startup+0x5c): undefined reference to `glutCreateWindow'
collect2: ld returned 1 exit status        

glut的实际lib文件(3.7.6,称为glut32.lib)位于mingw的lib文件夹中,而include文件位于include/GL中.

The actual lib file of glut (3.7.6, called glut32.lib) is in the lib folder of mingw and the include file in include/GL.

现在该怎么办?

推荐答案

尝试使用libglut.alibglut32.a而不是glut32.lib.要使用minGW编译glut程序.

Try it with libglut.a and libglut32.a instead of glut32.lib. To compile glut programs with minGW.

用于Win32的Do GLUT是原始GLUT库的Windows端口.它不再受到维护或支持. MinGW"w32api"软件包已随附两个GLUT导入库,即"libglut.a" "libglut32.a" ,但并未随附一个glut头文件.

Do GLUT for Win32 is a Windows port of the original GLUT library. It’s no longer maintained or supported. The MinGW "w32api" package already comes with two GLUT import libraries, "libglut.a" and "libglut32.a", but doesn’t come with a glut header file.

如果您曾经从互联网上下载过GLUT标头,并尝试将应用程序与这些导入库中的任何一个链接,则您可能会发现它因各种未定义的引用而失败.

If you’ve ever downloaded a GLUT header from the internet, and attempted to link an application against either of these import libraries, you likely would have seen it fail with various undefined references.

使用MinGW为Win32设置GLUT 寻找Setting Up GLUT for Win32 With MinGW

如果您想拥有自己的libglut32.a和glu32.dll构建".

  • Download Source glut-3.7.6-src.zip (4.76 MB)
  • Download makefile
  • Make the following modifications:

从第12行开始将以下行添加到include/GL/glut.h:

Add the following lines to include/GL/glut.h starting at line 12:

 #ifdef __MINGW32__
 #define APIENTRY __stdcall
 #define CALLBACK __stdcall
 #endif

在lib/glut/win32_winproc.c中注释掉第21行,使其看起来像:

Comment out line 21 in lib/glut/win32_winproc.c so that it looks:

//#include <crtdbg.h>

makefile第5行替换:

makefile line 5 replace:

-启用自动导入-启用自动导入

运行make

已使用链接Setting Up GLUT for Win32 With MinGW中的指南中的example.c文件进行了尝试.

Have tried it with the example.c file from the guidance in the link Setting Up GLUT for Win32 With MinGW.

具有预构建库自编译库.两者均可.

下面是结果.

示例GLUT应用程序

example.c来自Setting Up GLUT for Win32 With MinGW上方的链接.

example.c from the link above Setting Up GLUT for Win32 With MinGW.

#include <stdlib.h>
#include <GL/glut.h>

void keyboard(unsigned char key, int x, int y);
void display(void);


int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutCreateWindow("GLUT Test");
  glutKeyboardFunc(&keyboard);
  glutDisplayFunc(&display);
  glutMainLoop();

  return EXIT_SUCCESS;
}


void keyboard(unsigned char key, int x, int y)
{
  switch (key)
  {
    case '\x1B':
      exit(EXIT_SUCCESS);
      break;
  }
}


void display()
{
  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(1.0f, 0.0f, 0.0f);

  glBegin(GL_POLYGON);
    glVertex2f(-0.5f, -0.5f);
    glVertex2f( 0.5f, -0.5f);
    glVertex2f( 0.5f,  0.5f);
    glVertex2f(-0.5f,  0.5f);
  glEnd();

  glFlush();
}

这篇关于GLUT程序链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆