首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用freeglut编译问题

使用freeglut编译问题
EN

Stack Overflow用户
提问于 2015-10-18 01:56:03
回答 2查看 1.9K关注 0票数 3

前言

因此,我最近一直在用代码::块编写c++,最近在编译包含freeglut的项目时遇到了编译问题。我在windows 8上,我已经按照代码::块wiki正确地安装了自由落体。我还安装了git以及mingw64。问题是,我无法编译我的教授(使用freeglut的项目)使用代码::块或使用gitbash中的g++命令编写的源代码。

main.cpp:

代码语言:javascript
复制
/* 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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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);

makefile

代码语言:javascript
复制
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、include和bin内容文件添加到C下的正确目录中:\usr\local\mingw32 32(inlcude\,lib\,bin)和GL\Glu.h确实存在.

$ make c:\usr\local\mingw32\bin\g++.exe -c main.cpp -o main.o-i/usr/local/include/GL包含在main.cpp:7:0: Graphics.h:1:21:致命错误:GL/Glu.h:没有终止这样的文件或目录编译。makefile:16:目标' main.o‘失败的配方:*main.o错误1

代码中的编译::块

  1. 我以代码::Block打开main.cpp ; Graphics.h ; Graphics.cpp
  2. 使用main.cpp活动,我将“构建并运行”。--我得到了这个错误,似乎什么也没有发生,只是现在在项目目录中有一个main.o。

C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp||未定义引用到\_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||未定义引用\_imp\_\_\_\_glutCreateMenuWithExit@8' C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference toglClear@4’C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp||未定义引用setColor(ColorName)' C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference todrawFilledTriangle(int,int,C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp||未定义引用setColor(ColorName)' C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference todrawFilledCircle(int,int,int) C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp||未定义引用glFlush@0' C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp|| undefined reference tographicsSetup(int,char**,C:\Users\Admin\Desktop\C++Projects\cosc1337.code\GraphicsLib\examples\ex4\main.o:main.cpp||对`_imp__glutMainLoop@0‘===构建的未定义引用失败: 11错误,0警告(S) (0分钟(S),0秒(S)) ===

  1. 与上面一样,当Graphics.cpp是活动的,而我是build and run时,我得到了与前面相同的错误,但是Graphics.o是创建的。

结束这个:

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

编辑

我编写的所有其他程序以及“hello”编译在代码::块和gitbash上都没有问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-18 14:39:24

建筑物内控制台

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

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

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

代码语言:javascript
复制
PATH=c:\MinGW\mingw-w64\i686-5.1.0-posix-dwarf-rt_v4-rev0\mingw32\bin;%PATH%

假设二进制免费3.0.0 MinGW软件包被解压缩到c:\freeglut文件夹中。

代码语言:javascript
复制
::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二进制文件。

代码::积木项目

代码::块IDE可以被调优,以使过剩的项目模板与freeglue一起工作,如与代码::块一起使用FreeGlut所描述的。

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

现在可以使用GLUT project预置创建一个新项目。它只会问你glut路径。代码::块自动创建带有所需编译器选项(头路径)和链接器选项(路径和库)的项目。它还提供了可以按原样编译的main.cpp文件的基本示例。

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

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

Stack Overflow用户

发布于 2015-10-18 02:18:54

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

在代码::块中,您可以通过转到Project>Build选项,然后转到linker选项卡来添加Glu32.lib。在add下面单击link libraries。然后给它一个.lib文件的路径。确保不要使用相对路径。你想要字面上的路径。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33193516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档