Wade G
Wade G

Reputation: 105

Compiling Multiple C++ OpenGL

I'm writing a simple C++, opengl program and I'm having difficulty producing the executable from the object files. I can create object files of each .cpp without error using:

g++ -c filename.cpp  ---->   filename.o

Each .cpp creates an object, which leads me to assume that it is finding the opengl headers fine, but when I attempt to create the executable from the objects, it outputs:

g++ -o OUTPUT Main.o Time.o Map.o Vehicle.o

Main.o: In function `init()':
Main.cpp:(.text+0x12a): undefined reference to `glEnable'
Main.cpp:(.text+0x134): undefined reference to `glShadeModel'
Main.cpp:(.text+0x14d): undefined reference to `glViewport'
Main.cpp:(.text+0x157): undefined reference to `glMatrixMode'
Main.cpp:(.text+0x15c): undefined reference to `glLoadIdentity'

I realize that the g++ is telling me that it can't find the OpenGL functions anywhere, which means it is not linking to glu.h, gl.h etc. But why do the objects files get created without similar error, but I can't make my executable? Wouldn't the object files suffer from the same linking error as the .exe? I've tried -I and g++ -o RUN Time.o Main.o Map.o Vehicle.o -L/usr/include/GL/ -lGL -lGLU, and I've also verified the path of the OpenGL directory.

The headers of each files are below:

libgl.h:

#ifndef LIBGL_H
#define LIBGL_H

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "Time.h"
#include "Link.h"

#endif

Time.h:

#ifndef TIME_H
#define TIME_H

extern float GLOBAL_TIME;

#endif

Map.h

#ifndef MAP_H
#define MAP_H
#include "libgl.h"
....
#endif

Vehicle.h:

#ifndef VEHICLE_H
#define VEHICLE_H
#include "libgl.h"
...
#endif

Main.cpp:

#include "libgl.h"
#include "Map.h"
#include "Vehicle.h"
 ...

There is something that I'm missing regarding my headers/linking and compiling, if anybody has any suggestions I would be much obliged.

Upvotes: 0

Views: 1454

Answers (1)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234424

You need to link with the OpenGL libraries: probably pass -lgl to the last g++ invocation.

If your system has pkg-config you can use pkg-config --cflags --libs gl to get all the right flags.

For future reference, an "undefined reference" is a linker error, and it always means the same thing: you missed an object file or a library. It only shows up when linking, never when simply compiling (i.e. using -c)

Upvotes: 3

Related Questions