Compiling OpenGL programs on Windows, Linux and OS X
Here’s a simple OpenGL program that does nothing:
#include <GL/gl.h>
#include <SDL2/SDL.h>
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Window *mainWindow = SDL_CreateWindow("Tutorial 1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext mainGLContext = SDL_GL_CreateContext(mainWindow);
SDL_GL_MakeCurrent(mainWindow, mainGLContext);
SDL_Quit();
return 0;
}
On Mac OS X you need to change the #include directives slightly to:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
and compile it by typing (in a terminal window):
gcc -o hello hello.c -framework Carbon -framework OpenGL -framework SDL2
Using a makefile
You are required to use a makefile to compile your programs for all assignments. This makes it easy to recompile and test your program without typing the long gcc commands above.
A makefile is always called Makefile (with no extension). A simple makefile for Linux is:
CFLAGS = `sdl2-config --cflags`
LIBS = -lGL -lGLU `sd2-config --libs`
hello: hello.c
gcc -o hello hello.c $(CFLAGS) $(LIBS)
You must use a tab to indent the gcc
line (spaces will not work).
Writing for Windows, OS X and Linux
It can be troublesome rewriting your program every time you switch computers. Follow these instructions to have your programs and makefiles work on any computer, regardless of what operating system it’s running.
Use the following include
directives:
#define GL_GLEXT_PROTOTYPES
#if _WIN32
# include <Windows.h>
#endif
#if __APPLE__
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
#else
# include <GL/gl.h>
# include <GL/glu.h>
#endif
#include <SDL2/SDL.h>
And use the following in your makefile:
# Linux (default)
BIN = assignment
LIBS = -lGL -lGLU `sdl2-config --libs`
CFLAGS = -Wall -std=c99 `sdl2-config --cflags`
# OS X
ifeq "$(OSTYPE)" "darwin"
LIBS = -framework Carbon -framework OpenGL -framework SDL2
CFLAGS += -D__APPLE__
endif
Remember to change the BIN
exe as appropriate
Compiling at home on OS X
Install the developer tools (they are included amongst the installation DVDs). This includes everything you need so that the Makefile above will work fine.
Compiling at home on Linux
Some Linux distributions (e.g., Ubuntu) require you to install special -dev
packages alongside the main ones. For example, to get the OpenGL header files, you might need to look for an opengl-dev
package.
There are too many distributions around for us to give you any more guidance than this. If you are having trouble finding a dependency, try Googling for compile opengl <your distro>
, or post a message to the RTR discussion group.
Compiling at home on Windows using Visual C++ 2013
Microsoft provide a free compiler and IDE for Windows called Visual C++ 2013 Express Edition. You can download it from the VS Express site (it’s a big download).
These instructions apply to the Windows Desktop version of Visual C++ 2013, as well as 2012.
You’ll need to download the SDL2 library for Windows. Extract the contents of this ZIP file anywhere, and remember the location of the header files (.h
) and library files (.lib
) for later. Place any .dll
files in your Windows\System32
folder, or the folder where you plan to run any executables you compile.
In Visual C++, add the location of the SDL2 files to your project by following these steps:
- Right-click your project and go to
Properties
- Go to
C/C++
, thenGeneral
- Under
Additional Include Directories
, add the location of your SDL2 header (.h
) files (usually just the SDL2 folder is sufficient) - Go to
Linker
, thenGeneral
- Under
Additional Library Directories
, add the location of your SDL2 library (.lib
) files - Go to
Input
- Under
Additional Dependencies
, add the following to the end:;opengl32.lib;glu32.lib;SDL2.lib;SDL2main.lib
- Make sure you create your project as a Window application, as it is required by SDL.
You may need to also download the glew library as it contains OpenGL2+ functionality not provided by default on Windows. To use the library follow the same steps as you did for using SDL2.
You must be careful to keep a UNIX Makefile up-to-date and check your work in Sutherland regularly. Students who develop solely on Windows often find that compatibility problems cause major headaches when it comes to submitting their assignments. Remember that if your assignment doesn’t compile and run in the Sutherland lab, you will likely receive zero marks.