使用 freeglut 编译问题

Compiling Problems using freeglut

前言

所以我最近一直在用 Code::Blocks 编写 C++ 程序,最近在尝试编译包含 freeglut 的项目时 运行 遇到了编译问题。我在 windows 8,我已经根据 code::blocks wiki 正确安装了 freeglut。我还安装了 git bash 以及 mingw64。问题是我无法编译我的教授(使用 freeglut 的项目)使用 code::blocks 或在 gitbash 中使用 g++ 命令编写的源代码。

main.cpp:

/* Copyright (c) Mark J. Kilgard, 1996. */

/* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */

#include "Graphics.h"
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  setColor(RED);
  drawFilledTriangle(0,0,100,100,200,0);
  setColor(BLUE);
  drawFilledCircle(200,200,150);
  glFlush();  /* Single buffered, so needs a flush. */
}

int main(int argc, char **argv)
{
  graphicsSetup( argc, argv, &display);
  glutMainLoop();
  return 0;
}

Graphics.cpp

#include <iostream>
using namespace std;

#include "Graphics.h"
#include <cmath>
const double PI = acos(-1.0);
const double ANGLE_STEP      = PI/180.0;
void keyboard(unsigned char key, int x, int y)
{
    if (key == 27)
        exit(1);
}

void clearWindow() {
    glClear( GL_COLOR_BUFFER_BIT );
}

void setColor(ColorName name) {
    switch(name) {
        case WHITE: glColor3d(1.0, 1.0, 1.0); break;
        case GREY: glColor3d(0.4, 0.4, 0.4); break;
        case BLACK: glColor3d(0.0, 0.0, 0.0); break;
        case RED: glColor3d(1.0, 0.0, 0.0); break;
        case ORANGE: glColor3d(1.0, 0.5, 0.0); break;
        case YELLOW: glColor3d(1.0, 1.0, 0.0); break;
        case GREEN: glColor3d(0.0, 1.0, 0.0); break;
        case FOREST_GREEN: glColor3d(0.0, 0.25, 0.0); break;
        case BLUE: glColor3d(0.0, 0.0, 1.0); break;
        case CYAN: glColor3d(0.0, 1.0, 1.0); break;
        case MIDNIGHT_BLUE: glColor3d(0.0, 0.0, 0.25); break;
        case PURPLE: glColor3d(0.5, 0.25, 0.0); break;
        case MAGENTA: glColor3d(1.0, 0.0, 1.0); break;
        case BROWN: glColor3d(0.36, 0.16, 0.1); break;
        default: cerr << "Trying to set invalid color. Color unchanged" << endl;
    }
}
void graphicsSetup(int argc, char *argv[], void (*display)()) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB);
  if (! glutGet(GLUT_DISPLAY_MODE_POSSIBLE))
     glutInitDisplayMode(GLUT_INDEX);
  glutInitWindowPosition(50,50);
  glutInitWindowSize(640, 480);
  glutCreateWindow("single triangle");
  glClearColor(1.0,1.0,1.0,0.0);
  glutDisplayFunc(display);

  // set "esc" key to end program
  glutKeyboardFunc(keyboard);

  // set coordinates to "normal"
  glLoadIdentity();
  glOrtho(0,640,0,480, 1, -1);
}

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
     glBegin(GL_LINE_STRIP);
     glVertex2i(x1,y1);
     glVertex2i(x2,y2);
     glVertex2i(x3,y3);
     glVertex2i(x1,y1);
     glEnd();
}

void drawFilledTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
     glBegin(GL_POLYGON);
     glVertex2i(x1,y1);
     glVertex2i(x2,y2);
     glVertex2i(x3,y3);
     glVertex2i(x1,y1);
     glEnd();
}

void drawLine(int x1, int y1, int x2, int y2) {
     glBegin(GL_LINE_STRIP);
     glVertex2i(x1,y1);
     glVertex2i(x2,y2);
     glEnd();
}

void drawBox(int x1, int y1, int x2, int y2) {
     glBegin(GL_LINE_STRIP);
     glVertex2i(x1,y1);
     glVertex2i(x2,y1);
     glVertex2i(x2,y2);
     glVertex2i(x1,y2);
     glVertex2i(x1,y1);
     glEnd();
}

void drawFilledBox(int x1, int y1, int x2, int y2) {
     glBegin(GL_POLYGON);
     glVertex2i(x1,y1);
     glVertex2i(x2,y1);
     glVertex2i(x2,y2);
     glVertex2i(x1,y2);
     glVertex2i(x1,y1);
     glEnd();
}

void drawCircle(int x, int y, int radius) {
     double angle;
     int X, Y;
     glBegin(GL_LINE_STRIP);
     for (angle=0;angle< 2.0*PI + ANGLE_STEP; angle += ANGLE_STEP) {
         X = x + int(double(radius) * cos(angle));
         Y = y + int(double(radius) * sin(angle));
         glVertex2i(X,Y);
     }
     glEnd();
}         

void drawFilledCircle(int x, int y, int radius) {
     double angle;
     int X0, Y0, X1, Y1;
     glBegin(GL_TRIANGLES);
     X1 = x + radius;
     Y1 = y;
         for (angle=0;angle< 2.0*PI + ANGLE_STEP; angle += ANGLE_STEP) {
             X0 = X1;
             Y0 = Y1;
             X1 = x + int(double(radius) * cos(angle));
             Y1 = y + int(double(radius) * sin(angle));
             glVertex2i(x,y);
             glVertex2i(X0,Y0);
             glVertex2i(X1,Y1);
         }
         glEnd();
    }    

Graphics.h

#include <GL/glut.h>

// set the pre-defined colors
enum ColorName {
    WHITE,
    GREY,
    BLACK,
    RED,
    ORANGE,
    YELLOW,
    GREEN,
    FOREST_GREEN,
    BLUE,
    MIDNIGHT_BLUE,
    CYAN,
    PURPLE,
    MAGENTA,
    BROWN,
    NUM_COLORS
};

void clearWindow();
void setColor(ColorName name);
void graphicsSetup(int argc, char *argv[], void (*display)());

// graphic object primatives
void drawTriangle(int x1, int y1,int x2,int y2,int x3,int y3);
void drawLine(int x1, int y1, int x2, int y2);
void drawBox(int x1, int y1, int x2, int y2); // x-y coords of upper-right     and lower-left corners
void drawCircle(int x, int y, int radius);

// filled graphics primatives
void drawFilledTriangle(int x1, int y1,int x2,int y2,int x3,int y3);
void drawFilledBox(int x1, int y1, int x2, int y2);
void drawFilledCircle(int x, int y, int radius);

生成文件

TARGET = test.exe

CXX   = c:\usr\local\mingw32\bin\g++.exe
FILES = main.cpp Graphics.cpp
OBJS = $(FILES:.cpp=.o)

GINCS   = -I/usr/local/include/GL 
GLIBS   = -L/usr/local/lib -lfreeglut -lglu32 -lopengl32 -Wl,--subsystem,windows

all:  $(TARGET)

$(TARGET):  $(OBJS)
    $(CXX) -o $(TARGET) $(OBJS) $(GLIBS)

%.o:    %.cpp
    $(CXX) -c $< -o $@ $(GINCS)

clean:
    del $(TARGET) $(OBJS)

正在编译 GIT

  1. cd进入包含main.cpp ; Graphics.cpp ; Graphics.h ; makefile的目录。

  2. 然后我 运行 make 并得到以下结果,即使我已经将所有 freeglut lib、include 和 bin 内容文件添加到 C:[ 下的正确目录中=93=](inlcude\,lib\,bin) 和 GL\glut.h 肯定在那里...

$ make
c:\usr\local\mingw32\bin\g++.exe -c main.cpp -o main.o -I/usr/local/include/GL
In file included from main.cpp:7:0:
Graphics.h:1:21: fatal error: GL/glut.h: No such file or directory
compilation terminated.
makefile:16: recipe for target 'main.o' failed
make: *** [main.o] Error 1

正在编译 CODE::BLOCKS

  1. 我在code::blocks中打开main.cpp ; Graphics.h ; Graphics.cpp
  2. 随着 main.cpp 活动,我 "build and run"。 我收到这个错误,但似乎什么也没发生,除了现在项目目录中有一个 main.o。
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `_imp____glutInitWithExit@12'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `_imp____glutCreateWindowWithExit@8'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `_imp____glutCreateMenuWithExit@8'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `glClear@4'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `setColor(ColorName)'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `drawFilledTriangle(int, int, int, int, int, int)'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `setColor(ColorName)'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `drawFilledCircle(int, int, int)'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `glFlush@0'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `graphicsSetup(int, char**, void (*)())'
C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference to `_imp__glutMainLoop@0'
=== Build failed: 11 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===
  1. 与上面相同,当 Graphics.cpp 处于活动状态并且我 build and run 时,我得到与以前相同的错误,但是创建了 Graphics.o。

总结一下:

在我使用 code::blocks 创建了目标文件之后,我就可以使用 gitbash 和 运行 生成文件来link 他们,它确实创建了一个工作 test.exe,我可以 运行 并查看我的位置,但我感到困惑和沮丧,我不能只使用 code::blocks 或只是 git,因为这样做会使对代码的调整花费不合理的时间才能令人满意,并且肯定会在以后引起问题

编辑

我写的所有其他Programs,以及'hello world'在code::blocks和gitbash.[=25=上编译没有问题]

在你的 make 文件中,我看到你正在 link 使用像 -lfreeglut 这样的标志。我使用 linux 并且我记得必须将所有 l 放在我的 link 选项前面,特别是 linux。尝试使用 -freeglut 等。对于其他人。

在 code::blocks 中,您可以通过转到 Project>Build 选项添加 glut32.lib,然后转到 linker 选项卡。点击link libraries下的add。然后给它你的 .lib 文件的路径。一定不要使用相对路径。你想要文字路径。

我唯一的其他建议是#include <windows.h>。通常当你得到大量这样的未定义引用时,这是某种 linking 错误。

在控制台中构建

使用 Makefile 构建项目包括两个步骤:

  • 将源文件编译为二进制 object 个文件
  • object 文件与可执行文件的链接

可以仅使用纯 cmd 控制台中的 MinGW 来完成。唯一的要求是将 PATH 变量设置为指向 MinGW 二进制文件。它只能在该控制台本地完成,例如:

PATH=c:\MinGW\mingw-w64\i686-5.1.0-posix-dwarf-rt_v4-rev0\mingw32\bin;%PATH%

假设二进制文件 freeglut 3.0.0 MinGW Package 被提取到文件夹 c:\freeglut

::compilation
g++ -c main.cpp -o main.o -I"c:\freeglut\include"
g++ -c Graphics.cpp -o Graphics.o -I"c:\freeglut\include"

::linkage
g++ Graphics.o main.o -o test -L"c:\freeglut\lib" -lfreeglut -lglu32 -lopengl32 -Wl,--subsystem,windows

在编译期间生成 main.oGraphics.o object 个文件。

在您的情况下,错误出在这一步。它与 header 路径有关。通过列出 ls /usr/local/include/ 来检查 gitbash 路径 /usr/local/include/ 是否确实存在。如果它存在,请注意在您的源文件中 header 包含为 GL/glut.h 并且 GL 目录也存在于 GINCS 中。如果 header 路径是 /usr/local/include/GL/glut.h,这是一个错误。因此,从 GINCS.

中删除 GL

链接步骤生成目标 test.exe 二进制文件。

Code::Blocks 项目

可以调整 Code::Blocks IDE 使 GLUT 项目模板与 freeglue 一起工作,如 Using FreeGlut with Code::Blocks.

中所述

只需要在向导脚本glut\wizard.script和项目模板glut.cbp中将Glut32替换为freeglut即可。

现在可以使用 GLUT project 预设创建新项目。它只会问你 glut 路径。然后 Code::Blocks 自动创建配置了所需编译器选项(header 路径)和链接器选项(路径和库)的项目。它还提供了 main.cpp 文件,其中包含可以按原样编译的基本示例。

看起来您创建的不是 GLUT project,而是一个普通的空应用程序项目。要使其工作,您需要配置 Project build options:

  • Search directories:在Compiler选项卡(c:\freeglut\include)和Linker选项卡(c:\freeglut\lib)中添加路径;
  • Linker settings:添加不带l前缀的所需库名称(freeglutglu32opengl32)。