Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FreeGLUT linking Issues in Linux

I am running Linux Mint 14.1 64-bit

I have installed the following libs:

mesa-common-dev, freeglut3-dev, libglew-dev

through the apt-get tool.

Here are my includes, located in my Main.h file:

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stdio.h>

#include <GL/glew.h>
#include <GL/glut.h>
#include <time.h> 

I checked that the libs installed correctly, they seem to be located in /usr/lib/x86_64-linux-gnu and the headers in /usr/include/GL

I proceed to compile my Main.C file with the following flags:

g++ -Wall -Wextra -Weffc++ -Winit-self -Wmissing-include-dirs -Wswitch-default -switch-enum -Wunused-parameter -Wstrict-overflow=5 -Wfloat-equal -Wshadow -Wc++0x-compat -Wconversion -Wsign-conversion -Wmissing-declarations -Wstrict-null-sentinel -Woverloaded-virtual -Wsign-promo -Werror -pedantic -Wcast-qual -fno-pretty-templates -fmessage-length=80 -fdiagnostics-show-option -g -std=c++0x -pipe -frepo -c  Main.C -o Main.o

Main.o is generated without any issues, then I try to create the binary:

g++  -I/usr/include -L/usr/lib/x86_64-linux-gnu -lGL -lglut -lGLU -lGLEW -lX11 -lm -lrt -lpng Main.o -o main

And receive the following errors:

Main.o: In function `init()':
/path/to/Main.C:12: undefined reference to `glClearColor'
Main.o: In function `initGLUT(int, char**)':
/path/to/Main.C: undefined reference to `glutInit'
/path/to/Main.C:21: undefined reference to `glutInitDisplayMode'
/path/to/Main.C:24: undefined reference to `glutInitWindowSize'
/path/to/Main.C:25: undefined reference to `glutCreateWindow'
/path/to/Main.C:28: undefined reference to `glutDisplayFunc'
/path/to/Main.C:31: undefined reference to `glutKeyboardFunc'
/path/to/Main.C:34: undefined reference to `glutMouseFunc'
/path/to/Main.C:37: undefined reference to `glutReshapeFunc'
/path/to/Main.C:40: undefined reference to `glutIdleFunc'
Main.o: In function `printFPS()':
/path/to/Main.C:96: undefined reference to `glutGet'
Main.o: In function `reshape(int, int)':
/path/to/Main.C:123: undefined reference to `glutPostRedisplay'
Main.o: In function `getTime()':
/path/to/Main.C:129: undefined reference to `glutGet'
Main.o: In function `idle()':
/path/to/Main.C:141: undefined reference to `glutPostRedisplay'
Main.o: In function `display()':
/path/to/Main.C:148: undefined reference to `glClearColor'
/path/to/Main.C:149: undefined reference to `glClear'
/path/to/Main.C:150: undefined reference to `glFlush'
/path/to/Main.C:151: undefined reference to `glutSwapBuffers'
Main.o: In function `main':
/path/to/Main.C:164: undefined reference to `glutMainLoop'

The program compiles and links on another Linux system though. what could I be missing?

like image 706
FinalFortune Avatar asked Mar 04 '13 14:03

FinalFortune


2 Answers

You have to pass the libraries last (after the object file)

g++  -I/usr/include -L/usr/lib/x86_64-linux-gnu Main.o \
  -lGL -lglut -lGLU -lGLEW -lX11 -lm -lrt -lpng -o main

The reason behind this is, that the linker only links symbols, that are currently undefined. If you pass the libraries before the object files, then there aren't any undefined symbols to be linked and compilation/linking will therefore fail.

like image 161
knittl Avatar answered Oct 27 '22 12:10

knittl


The libraries need to come after your object files:

g++ -I/usr/include -L/usr/lib/x86_64-linux-gnu Main.o -lGL -lglut -lGLU -lGLEW -lX11 -lm -lrt -lpng  -o main
like image 21
nos Avatar answered Oct 27 '22 11:10

nos