使用freeglut编译问题

时间:2015-10-18 01:56:04

标签: c++ git makefile codeblocks 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:\ usr \ local \ mingw32下的正确目录中(inlcude \ ,lib \ n,bin)和GL \ glut.h在那里确定...

  3. $ 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
    

    编译代码:: BLOCKS

    1. 我在code :: blocks。
    2. 中打开main.cpp ; Graphics.h ; Graphics.cpp
    3. 当main.cpp处于活动状态时,我&#34;构建并运行&#34;。 我收到此错误,似乎没有任何事情发生,除了现在项目目录中有一个main.o。
    4. 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。
      2. 包装:

        在使用code :: blocks创建目标文件之后,我可以继续使用gitbash并运行makefile来链接它们,它确实创建了一个可以运行的工作test.exe并查看我的位置在哪里,但我感到困惑和沮丧,我不能只使用代码::块或只是git,因为这样做会使代码调整花费不合理的时间来获得满意,并且肯定会在以后导致问题< / em>的

        修改

        我写过的所有其他Programs以及“你好世界”#39;编译时没有问题代码:: blocks和gitbash。

2 个答案:

答案 0 :(得分:1)

在控制台中构建

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

  • 将源文件编译为二进制目标文件
  • 将目标文件链接到excecutable

可以使用纯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个目标文件。

在您的情况下,错误在此步骤。它与标题路径有关。通过列出gitbash,检查/usr/local/include/路径ls /usr/local/include/确实存在。如果存在则请注意,在源文件中,标题包含在GL/glut.h中,GL中也包含GINCS目录。如果标头路径为/usr/local/include/GL/glut.h,则会出错。因此,请从GL中删除GINCS

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

Code :: Blocks项目

可以调整Code :: Blocks IDE,使GLUT项目模板与freeglue中的Glut32一起使用。

只需在向导脚本freeglut和项目模板glut\wizard.script中将glut.cbp替换为GLUT project

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

看起来您创建的不是Project build options而是一个常规的空应用程序项目。要使其工作,您需要配置Search directories

  • Compiler:在c:\freeglut\include标签(Linker)和c:\freeglut\lib标签(Linker settings)中添加路径;
  • l:添加不含freeglut前缀(glu32opengl32-)的所需库名称。

答案 1 :(得分:0)

在您的make文件中,我看到您正在使用-lfreeglut之类的标记进行链接。我使用linux,我记得必须把所有的l都放在我的linux链接选项前面。尝试使用-freeglut等。为了其他人。

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

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