4

I'm trying to use the GLFW library, but am having difficulty with compiling a simple program. I went to the GLFW website and download the latest release, then using "How to build & install GLFW 3 and use it in a Linux project" I built and installed it.

Here's my code:

#include <GLFW/glfw3.h>
#include <iostream>
using std::cout;
using std::endl;

void GLFW_error(int error, const char* description)
{
    fputs(description, stderr);
}

void run()
{
    cout << "pooch" << endl;
}

int main()
{
    glfwSetErrorCallback(GLFW_error);
    if (!glfwInit()) exit(EXIT_FAILURE);

    run();

    glfwTerminate();
    return 0;
}

Using the command line:

bulletbill22@ROBOTRON ~/Desktop $ g++ -std=c++11 -lglfw source.cpp

yields

source.cpp:function main: error: undefined reference to 'glfwSetErrorCallback'

glfwSetErrorCallback is taken from their tutorial for "Setting an error callback".

Inclusion of -glfw3 results in /usr/bin/ld: error: cannot find -lglfw3

Even though everything seemed to be installed correctly, I suspect the problem may lie somewhere with the installation of the GLFW library because I'm not used to CMake and don't entirely understand how it works. I'm frustrated because the answer must be simple, but I'm not sure which keywords are really relevant when googling the problem; mostly the results are people who were incorrectly compiling with CMake, which I'm not compiling with in this case.

Community
  • 1
  • 1
  • I think you forgot to open shared library in the main function.Use `dlopen()` to open shared library. – Singh Jul 10 '14 at 06:34

1 Answers1

2

It seems that the directories for the glfw3.h header and libglfw3.so (and/or libglfw3.a) library are not in the default path.

You can check with by adding the -v option to the g++ options. Locate the directory where the glfw3.h header is found - call this $GLFW_INCDIR - it typically ends with .../GLFW. Locate the directory where the library is found - call this $GLFW_LIBDIR. Try:

g++ -std=c++11 -I$GLFW_INCDIR source.cpp -o pooch -L$GLFW_LIBDIR -lglfw3

If all the library dependencies are satisfied, this hopefully results in a program called pooch.

One other thing: GLFW3 is a C library, and the callback function arguments are expected to be C functions. So your callback should have 'C' linkage, i.e.,

extern "C" void GLFW_error(int error, const char* description) ...


Also, if you're having trouble with cmake, you may have ccmake installed. Try ccmake . in the top-level directory of the GLFW3 package for 'interactive' configuration.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90