5

我在识别我机器上的 GLFW 库的代码块 10.05 时遇到了一些困难。当我创建一个空项目并复制粘贴此 GLFW 教程中的代码 >> http://content.gpwiki.org/index.php/GLFW:Tutorials:Basics

#include <stdlib.h>
#include <GL/glfw.h>

void Init(void);
void Shut_Down(int return_code);
void Main_Loop(void);
void Draw_Square(float red, float green, float blue);
void Draw(void);

float rotate_y = 0,
      rotate_z = 0;
const float rotations_per_tick = .2;

int main(void)
{
  Init();
  Main_Loop();
  Shut_Down(0);
}

void Init(void)
{
  const int window_width = 800,
            window_height = 600;

  if (glfwInit() != GL_TRUE)
    Shut_Down(1);
  // 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
  if (glfwOpenWindow(window_width, window_height, 5, 6, 5,
                     0, 0, 0, GLFW_WINDOW) != GL_TRUE)
    Shut_Down(1);
  glfwSetWindowTitle("The GLFW Window");

  // set the projection matrix to a normal frustum with a max depth of 50
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  float aspect_ratio = ((float)window_height) / window_width;
  glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);
  glMatrixMode(GL_MODELVIEW);
}

void Shut_Down(int return_code)
{
  glfwTerminate();
  exit(return_code);
}

void Main_Loop(void)
{
  // the time of the previous frame
  double old_time = glfwGetTime();
  // this just loops as long as the program runs
  while(1)
  {
    // calculate time elapsed, and the amount by which stuff rotates
    double current_time = glfwGetTime(),
           delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
    old_time = current_time;
    // escape to quit, arrow keys to rotate view
    if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS)
      break;
    if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
      rotate_y += delta_rotate;
    if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
      rotate_y -= delta_rotate;
    // z axis always rotates
    rotate_z += delta_rotate;

    // clear the buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // draw the figure
    Draw();
    // swap back and front buffers
    glfwSwapBuffers();
  }
}

void Draw_Square(float red, float green, float blue)
{
  // Draws a square with a gradient color at coordinates 0, 10
  glBegin(GL_QUADS);
  {
    glColor3f(red, green, blue);
    glVertex2i(1, 11);
    glColor3f(red * .8, green * .8, blue * .8);
    glVertex2i(-1, 11);
    glColor3f(red * .5, green * .5, blue * .5);
    glVertex2i(-1, 9);
    glColor3f(red * .8, green * .8, blue * .8);
    glVertex2i(1, 9);
  }
  glEnd();
}

void Draw(void)
{
  // reset view matrix
  glLoadIdentity();
  // move view back a bit
  glTranslatef(0, 0, -30);
  // apply the current rotation
  glRotatef(rotate_y, 0, 1, 0);
  glRotatef(rotate_z, 0, 0, 1);
  // by repeatedly rotating the view matrix during drawing, the
  // squares end up in a circle
  int i = 0, squares = 15;
  float red = 0, blue = 1;
  for (; i < squares; ++i){
    glRotatef(360.0/squares, 0, 0, 1);
    // colors change for each square
    red += 1.0/12;
    blue -= 1.0/12;
    Draw_Square(red, .6, blue);
  }
}

并使用代码块编译它会返回这个错误:

/home/user/Graphics/Project_2/test.c|27|undefined reference to `glfwInit'|
/home/user/Graphics/Project_2/test.c|30|undefined reference to `glfwOpenWindow'|
......
.....
....
...

但是当我创建一个简单的 *.c 文件并使用以下命令进行编译时:

gcc myprog.c -o myprog -lglfw -lGL -lpthread

它有效..如何使代码块与 GLFW 一起使用?谢谢

4

4 回答 4

2

请遵循以下程序。

  1. 首先从这里下载 GLFW 。
  2. 解压压缩文件
  3. 从包含文件夹中复制 glfw.h 文件并粘贴到文件夹“C:\Program Files\CodeBlocks\MinGW\include\GL”
  4. 从 lib_mingw 文件夹复制所有文件(libglfw.a 和 libglfwdll.a)并粘贴到文件夹“C:\Program Files\CodeBlocks\MinGW\lib”
  5. 从解压缩文件的 lib_mingw 文件夹中提取 glfw.dll 文件并将其粘贴到“C:\Windows\System32”文件夹中。如果您不喜欢将其保存在 system32 中,则可以将其与项目 exe 文件一起保存。
  6. 现在在 code::blocks 中创建 GLFW 项目时,为 glfw 位置提供路径 C:\Program Files\CodeBlocks\MinGW"
  7. 如果您再次感到困惑,那么请到这里一步一步地使用每一步的图像。
于 2012-09-18T02:20:43.863 回答
1

确保您拥有正确类型的 glfw 库。即用于您的 mingw 编译器的 x86 或 x64。8 小时搜索未定义的引用错误,即 glfw 的链接器问题。

于 2013-06-23T16:23:08.807 回答
0

请按照以下说明操作:http: //codeincodeblock.blogspot.com/2011/02/setup-glfw-project-in-codeblock.html

于 2011-12-29T12:59:36.630 回答
0

我之前在将 GLFW 导入代码块时遇到了类似的问题,最近我发现了一些对我有用的东西。

我可以看到您已经正确完成了标头安装,但我会将其包含在此响应中,以便您可以仔细检查一切是否处于最佳状态。

我还建议您使用 glfw.org 的“文档”选项卡中的示例代码。它对我来说非常有效,所以我认为它也可能对你有用。

由于您使用的是 GLFW,我将假设您想在您的应用程序中使用 OpenGL,因此我将在教程中包含 OpenGL 的安装


A. 创建相关文件夹

您需要做的第一件事是为系统上的库文件和头文件设置一个位置。您可以在驱动器上的指定位置创建这些文件夹,也可以在您的 code::blocks 项目中创建这些文件夹。

就我而言,我在我的 code::blocks 项目文件夹中创建了一个“lib”和“head”文件夹。


B. 下载和安装必要的媒体

GLFW:

  1. 前往 GLFW.org,然后点击“下载”。
  2. 单击“32 位 Windows 二进制文件”,然后在下载的 zip 文件中打开“glfw-version.bin.WIN32”。
  3. 打开“include”文件夹并将随附的 GLFW 文件夹复制到您在 A 部分中创建的“head”文件夹中。
  4. 再次打开“glfw-version.bin.WIN32”,然后选择与您的编译器对应的“lib”文件夹。因为我使用的是 MinGW GCC 编译器,所以我选择了“lib-mingw”。要找到您的编译器,请转到 code::blocks,单击设置,然后单击“编译器...”。您的编译器位于出现的窗口的“选定编译器”部分。
  5. 找到您将使用的“lib”文件夹后,打开它,并将其中的所有文件复制到您在 A 部分中创建的“lib”文件夹中。

发光(OpenGL):

  1. 转到链接
  2. 打开下载的 zip 中的文件夹。
  3. 转到“lib”,然后释放,然后是 Win32,并将位于那里的所有文件复制到您在步骤 A 中创建的“lib”文件夹中。
  4. 返回到下载的 zip 中的文件夹。
  5. 进入“include”目录,将“GL”文件夹复制到a部分创建的“head”文件夹中。

此时,您的“head”文件夹应包含“GLFW”和“GL”文件夹,“lib”文件夹应包含“glew32”、“glew32s”以及 GLFW 中用于编译器的所有库文件。


C. 配置你的 code::blocks 项目

  1. 打开项目后,右键单击工作区中的项目名称,然后点击“构建选项...
  2. 在出现的窗口左侧,确保选择了“调试”。
  3. 接下来单击“链接器设置”选项卡,并添加以下库:“glfw.3”、“gdi32”和“opengl32”。
  4. 接下来,在“搜索目录”选项卡中,选择“编译器”选项卡。
  5. 将路径添加到“head”文件夹。如果头文件夹是在您的项目文件夹中创建的,您只需键入“头”
  6. 同样在“搜索目录”选项卡中,选择“链接器”选项卡。
  7. 将路径添加到“lib”文件夹。同样,如果 lib 文件夹是使用您的项目文件夹创建的,您只需键入“lib”。
  8. 点击窗口底部的“确定”。

D. 构建并运行您的代码 :)!希望这可以帮助!

于 2016-04-25T06:02:33.140 回答