GLFW and codeblocks

5

我在使用CodeBlocks 10.05时遇到了一些困难,它无法识别我的计算机上的GLFW库。当我创建一个空项目,并复制粘贴这个代码,这个代码是从这个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);
  }
}

当我使用Codeblocks编译时,出现以下错误:

and compile it using codeblocks


/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

它起作用了。我该如何让CodeBlocks与GLFW一起工作?谢谢。

4个回答

2

按照以下步骤进行操作。

  1. 首先从这里下载GLFW。
  2. 解压缩zip文件。
  3. 将include文件夹中的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. 如果您仍然感到困惑,请点击这里查看每个步骤的图像化逐步过程。

GLFW的压缩包中包含两个MinGW文件夹:lib-mingwlib-mingw-w64。我应该使用哪一个?(在Windows 10 64位上使用从网络安装程序安装的MinGW(无论是哪个版本))。 - birgersp
目前使用glfw的CodeBlocks在尝试从模板创建项目时出现错误。 - marti_

1

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


我怀疑这就是我正在寻找的解决方案。我使用通过mingw安装程序安装的Windows 64位操作系统(那是32位还是64位?)。 - birgersp

0

我之前在将GLFW导入CodeBlocks时也遇到了类似的问题,最近我找到了一些解决方法。

我可以看到您已经正确安装了头文件,但我会在这个回复中包含它,以便您可以再次检查一切是否处于最佳状态。

我还建议您使用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”文件夹中。

GLEW(OpenGL):

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

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


C. 配置您的代码::blocks项目

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

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


0

5
欢迎来到 Stack Overflow!虽然这个回答理论上是正确的,但最好在这里包含答案的核心内容,并提供链接作为参考。 - Bill the Lizard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接