GLFW and codeblocks
Asked Answered
C

4

5

I am having some difficulties with codeblocks 10.05 recognizing the GLFW libraries on my machine. When I create an empty project, and copy paste this code found from this GLFW tutorial >> 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);
  }
}

and compile it using codeblocks it returns me this error:

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

but when I create a simple *.c file and compile it with :

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

it works.. How can I make codeblocks work with GLFW?? thanks

Cierracig answered 22/10, 2011 at 0:35 Comment(0)
T
2

Follow the following procedure.

  1. First download the GLFW from here.
  2. unzip the zip file
  3. copy the glfw.h file from the include folder and paste to the folder "C:\Program Files\CodeBlocks\MinGW\include\GL"
  4. copy all file (libglfw.a and libglfwdll.a ) from the lib_mingw folder and paste to the folder "C:\Program Files\CodeBlocks\MinGW\lib"
  5. glfw.dll file from the lib_mingw folder of unzip file and paste it inside "C:\Windows\System32" floder. If you don't like to keep it in system32 then you can keep it with your project exe file.
  6. Now while creating the GLFW project in code::blocks give the path C:\Program Files\CodeBlocks\MinGW" for glfw location
  7. IF you are again confuse then go here for step by step procedure with image of every step.
Tireless answered 18/9, 2012 at 2:20 Comment(2)
the GLFW zip contains two mingw folders: lib-mingw and lib-mingw-w64. Which one should I use? (Windows 10 64-bit using MinGW installed from the web installer (whatever version that is)).Hellen
currently codeblock with glfw had and error when try to create a project from templateAvoid
S
1

Make sure that you have the correct type of glfw libs. ie x86 or x64 for your mingw compiler. 8 Hours of time searching for undefined reference errors ie linker problems to glfw.

Sumba answered 23/6, 2013 at 16:23 Comment(1)
I'm supsecting this is the solution I'm looking for. I'm on Windows 64-bit with mingw installed through the mingw installer (is that 32 or 64-bit?).Hellen
A
0

Follow these instrucions: http://codeincodeblock.blogspot.com/2011/02/setup-glfw-project-in-codeblock.html

Aesop answered 29/12, 2011 at 12:59 Comment(1)
Welcome to Stack Overflow! While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Baluchi
S
0

I was having similar problems earlier with importing GLFW into codeblocks, and I recently found something that works for me.

I can see that you have already done the header installation correctly, but I will include that in this response so that you can double-check that everything is in tip-top condition.

I also recommend that you use the sample code within the 'Documentation' tab of glfw.org. It works really well for me, so I think it might for you as well.

Since you are using GLFW, I will assume that you want to use OpenGL in your application, so I will include the installation of OpenGL in the tutorial


A. Creating relevant folders

The first thing you will need to do is set up a location for library files and header files on your system. You can either creates these folders within a specified location on your drive, or you can create these folders within your code::blocks project.

In my case, I created a 'lib' and 'head' folder in my code::blocks project folder.


B. Downloading & Installing necessary media

GLFW:

  1. Head over to GLFW.org, and hit 'Downloads'.
  2. Click '32-bit Windows binaries', and open the 'glfw-version.bin.WIN32' within zip file that was downloaded.
  3. Open the 'include' folder and copy the enclosed GLFW folder to the 'head' folder you created in part A.
  4. Open up 'glfw-version.bin.WIN32' again, and pick the 'lib' folder that corresponds to your compiler. Because I am using the MinGW GCC compiler, I picked 'lib-mingw'. To find your compiler, go to code::blocks, click settings, and then click 'compiler...'. Your compiler is found in the 'selected compiler' section of the window that appears.
  5. Once you've found the 'lib' folder that you will use, open it, and copy all of the files within into the 'lib' folder that you created during part A.

GLEW (OpenGL):

  1. Go to this link
  2. Open the folder that is within the downloaded zip.
  3. Go to 'lib', then release, then Win32, and copy all of the files located there into the 'lib' folder you created in step A.
  4. Go back to the folder within the downloaded zip.
  5. Enter the 'include' directory, and copy the 'GL' folder into the 'head' folder created in part a.

At this point, your 'head' folder should contain 'GLFW' and 'GL' folders, and your 'lib' folder should contain 'glew32', 'glew32s', and all of the library files from GLFW for your compiler.


C. Configuring your code::blocks project

  1. Once you've opened your project, right click on the name of your project in your workspace, and hit 'build options...
  2. On the left of the window that has appeared, make sure 'Debug' is selected.
  3. Next click the 'linker settings' tab, and add the following libraries: 'glfw.3', 'gdi32', and 'opengl32'.
  4. Next, in the 'Search Directories' tab, select the 'Compiler' tab.
  5. Add the path to your 'head' folder. If the head folder was created within your project folder, you can just type 'head'
  6. Also in the 'Search Directories' tab, select the 'Linker' tab.
  7. Add the path to your 'lib' folder. Again, if the lib folder was created withing your project folder, you can just type 'lib'.
  8. Hit 'OK' at the bottom of the window.

D. Build and Run your code :)! Hope this helps!

Swash answered 25/4, 2016 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.