1

I'm trying to follow this tutorial for OpenGL. I originally copied the code by hand, but that wasn't working, so I've copy-pasted the code straight from the website. I keep getting this error:

[Linker error] undefined reference to 'glfwInit'

from this code (which feels longer than necessary):

//C++ standard headers
#include <stdio.h>
#include <stdlib.h>
//GLEW header
#include <GL/glew.h>
//GLFW header
#include <GL/glfw3.h>

int main()
{
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

    // Open a window and create its OpenGL context 
    GLFWwindow* window; // (In the accompanying source code, this variable is global) 
    window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL); 
    if( window == NULL )
    {
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
            glfwTerminate();
                return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW 
    glewExperimental=true; // Needed in core profile 
    if (glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do{
    // Draw nothing, see you in tutorial 2 !

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

    }
    // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
    glfwWindowShouldClose(window) == 0 );
}

I've got no idea why it's not compiling. Anyone know what's going on?
EDIT: I'm using Dev-C++, as stated in the title.

Kekker_
  • 21
  • 1
  • 2
  • This means the compiler cannot find `glfwInit` at link time, which means you are not correctly supplying the flags required for the compiler to find the library that contains it. – donkopotamus Oct 09 '15 at 02:00
  • To add to the previous comment: if you haven't installed the OpenGL libraries, you will need to. If you have installed them, you need to tell the linked where to find them. I'm not familiar with Dev-C++, but most compilers I know have a `-l` option to do this (that's an ell, not a one) and most IDEs have an option for this buried somewhere. – John Perry Oct 09 '15 at 02:41
  • @john OpenGL headers and libraries come with Dev C++, GLEW and GLFW didn't. I don't get any OpenGL errors, just GLFW. I think Blake found my problem, I'll have to test it out when I get the chance. – Kekker_ Oct 09 '15 at 11:14
  • What Blake is saying is the same as what I'm saying, except that I didn't realize GLFW is a different library from OpenGL. :-) Linking to libraries is one of the more maddening difficulties of C/C++. – John Perry Oct 09 '15 at 12:31

1 Answers1

2
undefined reference to 'glfwInit'

means that the linker did not find the library where glfwInit() is defined. You have to add glfw3.a to your linker input. Indeed, Dev-C++ use MinGW so unlike Visual Studio, the libraries can not be .lib.

To do that with Dev-C++, go to your 'project options', 'parameters', and 'add a library'. Then browse the explorer to find the glfw3.a I mentioned (usually in GLFW-<version>/lib/).

blakelead
  • 1,780
  • 2
  • 17
  • 28
  • Ah ok. I was thinking it was a header problem, so I was moving the header files around a lot. It's been years since I last used C++, I'd forgotten about libraries. I'll try this when I can, thank you. – Kekker_ Oct 09 '15 at 11:16
  • You are welcome. Hope it'll work – blakelead Oct 09 '15 at 11:17
  • I found two .a files, libglfw3.a and glfw3dll.a. The other libraries in Dev-C++'s /lib folder were lib.a, so I moved libglfw3 into the /lib folder, linked it in project options/parameters, but I'm still getting the same errors. – Kekker_ Oct 09 '15 at 14:42
  • If you use glew or glut (or both), the order of linking is important. Try changing the order. For example, gflw is dependent on glew if you have both. – blakelead Oct 09 '15 at 16:29
  • I'm not finding any .a glew files. Apparently I'm supposed to compile it from source myself, but I don't know how to do that and it sounds like such a hassle. Is there any easier to use alternatives? I saw gl3w used somewhere, maybe I can just use that instead? – Kekker_ Oct 10 '15 at 01:50
  • Building glew is not that hard, I can walk you through it if you want. Also you could use an other IDE. Dev-C++ is old and there are not as many resources and tutorials as for others. If you are on Windows, I suggest Visual Studio (express or community are free). There are already compiled builds for almost all versions. – blakelead Oct 10 '15 at 06:47
  • VS never worked on my computer, I'd rather not use that. I've been looking at other IDEs, it's tough to find one I like. How is GLEW compiled? – Kekker_ Oct 12 '15 at 21:45
  • Before trying to compile it, I forgot to ask you if you linked it correctly : under 'project options'->'parameters'->'linker' you need to add -lopengl32 -lglew32 if they are not there already. Now about compiling, I did a little research and found that it is not necessary for developping with dev-c++ : just download it, copy all contents to dev-c++ main directory and put the glew32.dll in Windows/system32 folder. – blakelead Oct 13 '15 at 06:56
  • Thanks for the info, I greatly appreciate it. I hadn't linked it correctly, I'll try that when I can. If it doesn't work, I'll just try it with another IDE. – Kekker_ Oct 14 '15 at 12:48