Sublime Forum

Building Glut with C++ in Sublime Text 2

#1

Hi guys,

I recently discovered sublime text 2 and i am loving it, i am starting to learn c++ and opengl so my query is how can i build a opengl code with cpp from sublime’s build menu, since i am trying to build the same and i get errors
i am using this Build File

[code]{
“cmd”: “g++”, “${file}”, “-o”, “${file_path}/${file_base_name}”],
“file_regex”: “^(…^:]):([0-9]+):?([0-9]+)?:? (.)$”,
“working_dir”: “${file_path}”,
“selector”: “source.c, source.c++”,

"variants":

	{
		"name": "Run",
		"cmd": "${file_path}/${file_base_name}.exe"]
	}
]

}[/code]
and building this cpp code

[code]#include <GL/glut.h>

void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0, 0, -30);

// Green Sphere
glPushMatrix();
    glColor3f(0.6, 0.9, 0);
    glTranslatef(7, -5, 0);
    glutSolidSphere(5, 50, 50);
glPopMatrix();

// Blue Cube
glPushMatrix();
    glColor3f(0.0, 0.5, 0.9);
    glTranslatef(0, 5, 0);
    glRotatef(45, 1, 0, 0);
    glRotatef(45, 0, 1, 0);
    glutSolidCube(8);
glPopMatrix();

// Violet Cone
glPushMatrix();
    glColor3f(0.7, 0.0, 1.0);
    glTranslatef(-7, -5, 0);
    glRotatef(-65, 0, 1, 0);
    glRotatef(-65, 1, 0, 1);
    glutSolidCone(5, 10, 100, 100);
glPopMatrix();

glFlush();
glutSwapBuffers();

}

void init(void)
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glClearDepth( 1.0 );
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_LIGHT0);
}

void reshape(int w, int h)
{
float aspectRatio = (float)w/(float)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspectRatio, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(400, 300);
glutCreateWindow(“GLUT on Sublime Text”);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(render);
glutMainLoop();
return 0;
}[/code]

i get long list of errors

C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x1c): undefined reference to `__glutInitWithExit' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x37): undefined reference to `__glutCreateWindowWithExit' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x52): undefined reference to `__glutCreateMenuWithExit' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x66): undefined reference to `_imp__glClear' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x6d): undefined reference to `_imp__glLoadIdentity' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x8e): undefined reference to `_imp__glTranslatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x95): undefined reference to `_imp__glPushMatrix' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0xb6): undefined reference to `_imp__glColor3f' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0xd7): undefined reference to `_imp__glTranslatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0xff): undefined reference to `glutSolidSphere' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x104): undefined reference to `_imp__glPopMatrix' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x10b): undefined reference to `_imp__glPushMatrix' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x12c): undefined reference to `_imp__glColor3f' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x14d): undefined reference to `_imp__glTranslatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x177): undefined reference to `_imp__glRotatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x1a1): undefined reference to `_imp__glRotatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x1b9): undefined reference to `glutSolidCube' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x1be): undefined reference to `_imp__glPopMatrix' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x1c5): undefined reference to `_imp__glPushMatrix' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x1e6): undefined reference to `_imp__glColor3f' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x207): undefined reference to `_imp__glTranslatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x231): undefined reference to `_imp__glRotatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x25b): undefined reference to `_imp__glRotatef' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x295): undefined reference to `glutSolidCone' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x29a): undefined reference to `_imp__glPopMatrix' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x2a1): undefined reference to `_imp__glFlush' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x2a8): undefined reference to `glutSwapBuffers' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x2d8): undefined reference to `_imp__glClearColor' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x2f0): undefined reference to `_imp__glClearDepth' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x2fe): undefined reference to `_imp__glEnable' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x30c): undefined reference to `_imp__glEnable' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x31a): undefined reference to `_imp__glShadeModel' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x328): undefined reference to `_imp__glEnable' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x33e): undefined reference to `_imp__glColorMaterial' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x34c): undefined reference to `_imp__glEnable' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x36d): undefined reference to `_imp__glMatrixMode' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x374): undefined reference to `_imp__glLoadIdentity' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x3b7): undefined reference to `_imp__gluPerspective' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x3c5): undefined reference to `_imp__glMatrixMode' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x3f5): undefined reference to `glutInitDisplayMode' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x409): undefined reference to `glutInitWindowSize' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x426): undefined reference to `glutReshapeFunc' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x432): undefined reference to `glutDisplayFunc' C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o:simpleGlut.cpp:(.text+0x437): undefined reference to `glutMainLoop' c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\HARIOM~1\AppData\Local\Temp\cceovWVy.o: bad reloc address 0x20 in section `.eh_frame' c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation collect2.exe: error: ld returned 1 exit status [Finished in 2.1s with exit code 1]

Kindly help me solve this problem
Thanks

0 Likes

#2

Well, the issue you’re having is that you’re not linking to GLUT and GL (and possibly other things). That said, you’re using deprecated GL functions (the fixed-function pipeline is all more or less gone, in favor of the programmable pipeline) and GLUT is almost entirely dead and a bad idea to use since it’s poorly designed. Really, almost nobody uses it. You might want to take a look at GLFW to replace it and familiarize yourself with 1) more C++ basics and 2) modern OpenGL, but that’s not what you’re asking.

Anyway, tell your compiler (to tell the linker) to link to your GLUT and GL libraries, assuming they’re already in its library search path (check the gcc man pages for all this stuff). Aside from that, the build system itself won’t work with anything past a single file, so you’ll probably also want to learn how to work with something like make or premake or CMake or scons (god forbid) or qmake (does that still exist?) or what have you. Again, though, the main thing is just that you’re forgetting to link libraries you’ve used.

0 Likes

#3

Thanks for your reply, I will definitely look at GLFW.
Cheers

0 Likes

#4

Hey man,
I’ve been… dabbling…? Umm… with OpenGL in Sublime Text recently too, and i can’t tell you how fickle compilers can be. Mainly when it comes to linking your libraries.

The order is very important, and my example programs weren’t compiling for that reason.

Try using make or cmake and life will be a lot easier.

CC = g++
CFLAGS = -Wall
PROG = myprogram

SRCS = main.cpp

ifeq ($(shell uname),Darwin)
	LIBS = -framework OpenGL -framework GLUT
else
	LIBS = -lGLU -lGL -lglut
endif

all: $(PROG)

$(PROG):	$(SRCS)
	$(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS)

clean:
	rm -f $(PROG)

This part -lGLU -lGL -lglut is where my problem was. If you’re still having troubles, try compiling it by linking in that order.
Anyway, stripped down this is how it works for me… and many others.

Good luck!

PS I am running a linux environment

0 Likes