Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: glfw3 on ubuntu undefined reference

I'm trying to compile a program from this tutorial.

#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>

static void error_callback(int error, const char* description)
{
 fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
 if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
 glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
 GLFWwindow* window;
 glfwSetErrorCallback(error_callback);
 if (!glfwInit())
 exit(EXIT_FAILURE);
 window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL); 
 if (!window)
 {
  glfwTerminate();
  exit(EXIT_FAILURE);
 }
 glfwMakeContextCurrent(window);
 glfwSetKeyCallback(window, key_callback);
 while (!glfwWindowShouldClose(window))
 {
  float ratio;
  int width, height;
  glfwGetFramebufferSize(window, &width, &height);
  ratio = width / (float) height;
  glViewport(0, 0, width, height);
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
  glBegin(GL_TRIANGLES);
  glColor3f(1.f, 0.f, 0.f);
  glVertex3f(-0.6f, -0.4f, 0.f);
  glColor3f(0.f, 1.f, 0.f);
  glVertex3f(0.6f, -0.4f, 0.f);
  glColor3f(0.f, 0.f, 1.f);
  glVertex3f(0.f, 0.6f, 0.f);
  glEnd();
  glfwSwapBuffers(window);
  glfwPollEvents();
 }
 glfwDestroyWindow(window);
 glfwTerminate();
 exit(EXIT_SUCCESS);
 }

What I get when I try to compile this:

/usr/local/lib/libglfw3.a(x11_window.c.o): In function `_glfwPlatformCreateCursor':
x11_window.c:(.text+0x2d91): undefined reference to `XcursorImageCreate' 
x11_window.c:(.text+0x2e61): undefined reference to `XcursorImageLoadCursor'
x11_window.c:(.text+0x2e75): undefined reference to `XcursorImageDestroy'

I use this command

g++ glfw.o -lglfw3 -lGL -lGLU -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lm -lalleg

Running on Ubuntu 13.10

I looked everywhere, installed box2d, glfw, glfw3, allegro, and some more libs - the reason is that I went through every discussion that remotely mentions glfw and undefined references, and in my frustration caused by five hours of stalemate, installed any library that was mentioned. The result remains the same. I don't know where to go from here. Any ideas what causes this please? Thank you.

like image 350
Kuro Avatar asked Jan 22 '26 10:01

Kuro


2 Answers

until mr. AndonM.Coleman posts an answer for this question ( which he did in the comment ), the problem was in missing -lXcursor

like image 196
Kuro Avatar answered Jan 24 '26 07:01

Kuro


Manually writing those commands out is a pain. You want to use pkg-config. Something like this (Makefile syntax):

LIBS := $(shell pkg-config --libs glfw3) -lm -lalleg
g++ $(LDFLAGS) glfw.o -pthread $(LIBS)

Also note use of -pthread instead of -lpthread.

Note about library order

Using GNU Binutils on Linux, the order in which you specify libraries is important. This is not true on OS X or Windows. Symbols are resolved by scanning libraries in order, so any unresolved symbols in a library must be resolved by a library which comes afterwards in the command line arguments.

Note that if you use static GLFW, you may need to use pkg-config --libs --static glfw3.

Terminology note

The command,

g++ file.o <flags>

does not compile code. The code is already compiled, this command links the compiled files.

like image 39
Dietrich Epp Avatar answered Jan 24 '26 08:01

Dietrich Epp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!