undefined reference to 'glutInitJoystick'

Hi, I’ve been getting this error message when trying to compile an opengl program with calls to glutInitJoystick("/dev/input/js0"); and glutJoystick(joystick, 10);

singlesine.c:(.text+0x171b): undefined reference to `glutInitJoystick’

I’m using Ubuntu 10.04 (Lucid Lynx) with glut 3.7 installed.

Can anyone tell me what this error message means, and how to fix it?

Thanks in advance

Interesting, I don’t think glut or freeglut ever had glutInitJoystick – it is not in my glut headers on ubuntu. A quick google search lead to a one off modified glut to include this at glutjoy. Maybe centOS used this rogue glut library? I am not familar with CentOS but have used Ubuntu+freeglut. A look at the header file for freeglut_std.h shows a “glutJoystickFunc” callback but note on X11 is discouraging. In other words I think GLUT is not so good for joystick controls.

As a low-level API, if you want to use the joystick in linux you could do something low level as

// g++ joystick.cpp
/* this is the linux 2.2.x way of handling joysticks. It allows an arbitrary
 * number of axis and buttons. It's event driven, and has full signed int
 * ranges of the axis (-32768 to 32767). It also lets you pull the joysticks
 * name. The only place this works of that I know of is in the linux 1.x 
 * joystick driver, which is included in the linux 2.2.x kernels

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>

#define JOY_DEV "/dev/input/js0"

int main()
	int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
	char *button=NULL, name_of_joystick[80];
	struct js_event js;

	if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
		printf( "Couldn't open joystick
" );
		return -1;

	ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
	ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
	ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );

	axis = (int *) calloc( num_of_axis, sizeof( int ) );
	button = (char *) calloc( num_of_buttons, sizeof( char ) );

	printf("Joystick detected: %s
	%d axis
	%d buttons

		, name_of_joystick
		, num_of_axis
		, num_of_buttons );

	fcntl( joy_fd, F_SETFL, O_NONBLOCK );	/* use non-blocking mode */

	while( 1 ) 	/* infinite loop */

			/* read the joystick state */
		read(joy_fd, &js, sizeof(struct js_event));
			/* see what to do with the event */
		switch (js.type & ~JS_EVENT_INIT)
			case JS_EVENT_AXIS:
				axis   [ js.number ] = js.value;
				button [ js.number ] = js.value;

			/* print the results */
		printf( "X: %6d  Y: %6d  ", axis[0], axis[1] );
		if( num_of_axis > 2 )
			printf("Z: %6d  ", axis[2] );
		if( num_of_axis > 3 )
			printf("R: %6d  ", axis[3] );
		for( x=0 ; x<num_of_buttons ; ++x )
			printf("B%d: %d  ", x, button[x] );

		printf("  \r");

	close( joy_fd );	/* too bad we never get here */
	return 0;

And note that freeglut_ext.h says

 * Joystick functions, see freeglut_joystick.c
/* If you have a serious need for these functions in your application, please either
 * contact the "freeglut" developer community at [email]freeglut-developer@lists.sourceforge.net[/email],
 * switch to the OpenGLUT library, or else port your joystick functionality over to PLIB's
 * "js" library.

You also might want to consider GLFW. I prefer it to GLUT, it has joystick support and is still being updated. It’s a very easy switch from GLUT to GLFW.