【问题标题】:Compiling Problems using freeglut使用 freeglut 编译问题
【发布时间】:2015-10-18 01:56:03
【问题描述】:

前言

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

ma​​in.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 下的正确目录中\,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

在代码中编译::块

  1. 我在 code::blocks 中打开 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|| 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 上运行 makefile 来链接它们,它确实创建了一个我可以运行并查看的工作 test.exe我的位置在哪里,但我很困惑和沮丧,因为我不能只使用 code::blocks 或只使用 git,因为这样做会使代码的调整花费不合理的时间才能令人满意,并且肯定会在以后引起问题

编辑

我写过的所有其他Programs,以及'hello world' 编译在code::blocks 和gitbash 上都没有问题。

【问题讨论】:

  • 这听起来像是您的 makefile 或您的 code::blocks 或两者都配置错误。我建议只尝试编译像int main() {} 这样的极其简单的程序,直到您能够可靠且一致地完成它,而不会出现两种方式的错误。只要您不能这样做,您的系统就需要一些工作。然后逐渐尝试包括免费的过剩等,直到你可以让它工作,如果你真的被卡在某个地方,再在 SO 上发帖。
  • 我编写的所有其他程序都可以在 code::blocks 和 git bash 中顺利编译。而且我总是确保 hello world 会从一开始就编译。

标签: c++ git makefile codeblocks freeglut


【解决方案1】:

在控制台中构建

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

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

可以只使用来自纯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.o 和Graphics.o 目标文件。

在您的情况下,此步骤出现错误。它与标题路径有关。通过列出ls /usr/local/include/ 来检查gitbash 中的路径/usr/local/include/ 确实存在。如果它存在,那么请注意,在您的源文件中,标头包含为GL/glut.h,并且GL 目录也存在于GINCS 中。如果标头路径为/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 自动创建配置了所需编译器选项(头文件路径)和链接器选项(路径和库)的项目。它还提供了main.cpp 文件以及可以按原样编译的基本示例。

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

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

【讨论】:

  • 谢谢。你是对的。但事实证明,问题在于我没有在 glut 项目模板中构建文件。
【解决方案2】:

在您的 make 文件中,我看到您使用 -lfreeglut 之类的标志链接。我使用 linux,我记得必须将所有 l 放在我专门针对 linux 的链接选项的前面。尝试使用-freeglut 等。给其他人。

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

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

【讨论】:

  • 我现在将编辑 makefile 并尝试编译。我希望就是这样,因为这些源文件直接来自我的教授,我确实必须更改一些 makefile,但没什么大不了的。根据您的 code::blocks 建议,code::blocks wiki 指导让我浏览了 glut 的 wizard.script 并将所有对 glut32 的引用更改为 freeglut。我不确定这会奏效,但我现在愿意尝试任何事情。谢谢,稍后会回来
  • 更改了我的makefile并取出了引用前面的l,仍然存在同样的错误。经检查(code::blocks 的 mingw 和我的 mingw64 lib 路径)我没有看到任何 glut32.lib 或 glut32.a,但 freeglut.a 文件在两者中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 2012-05-25
  • 1970-01-01
相关资源
最近更新 更多