编译 使用自由过剩的问题

Compiling Problems using freeglut

本文关键字:问题 自由 编译      更新时间:2023-10-16

前言

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

主.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;
}

图形.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();
    }    

图形.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:usrlocalmingw32bing++.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(include\,lib\,bin( 下的正确目录中,并且 GL\glut.h 肯定在那里......

$ make
c:usrlocalmingw32bing++.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. 我在代码中打开main.cpp ; Graphics.h ; Graphics.cpp::块。
  2. 在主要.cpp活动的情况下,我"构建并运行"。我收到此错误,似乎什么也没发生,除了现在项目目录中有一个 main.o。
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `_imp____glutInitWithExit@12'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `_imp____glutCreateWindowWithExit@8'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `_imp____glutCreateMenuWithExit@8'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `glClear@4'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `setColor(ColorName)'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `drawFilledTriangle(int, int, int, int, int, int)'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `setColor(ColorName)'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `drawFilledCircle(int, int, int)'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `glFlush@0'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `graphicsSetup(int, char**, void (*)())'
C:UsersAdminDesktopC++Projectscosc1337.codeGraphicsLibexamplesex4main.o:main.cpp|| undefined reference to `_imp__glutMainLoop@0'
=== Build failed: 11 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===
  1. 与上述相同的事情,当图形.cpp处于活动状态并且我build and run时,我收到与以前相同的错误,但是创建了Graphics.o。

总结一下:

在我使用 code::

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

编辑

我编写的所有其他程序,以及"hello world"编译,在代码::blocks和gitbash上没有问题。

在控制台中构建

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

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

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

PATH=c:MinGWmingw-w64i686-5.1.0-posix-dwarf-rt_v4-rev0mingw32bin;%PATH%

假设二进制 freeglut 3.0.0 MinGW 包被解压缩到文件夹 c:freeglut

::compilation
g++ -c main.cpp -o main.o -I"c:freeglutinclude"
g++ -c Graphics.cpp -o Graphics.o -I"c:freeglutinclude"
::linkage
g++ Graphics.o main.o -o test -L"c:freeglutlib" -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,则为错误。因此,请从GINCS中删除GL

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

代码::块项目

可以调整 Code::Blocks IDE,使 GLUT 项目模板能够与 freeglue 一起使用,如将 FreeGlut 与 Code::Blocks 结合使用中所述。

只需要在向导脚本glutwizard.script和项目模板glut.cbpfreeglut替换Glut32即可。

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

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

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

在您的制作文件中,我看到您正在链接像 -lfreeglut 这样的标志。我使用 linux,我记得必须将所有 l 放在我的 linux 链接选项前面。尝试使用-freeglut等。对于其他人。

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

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