Adapting Your Makefile To Work On Any Operating System
Like most developers, I test my code on multiple different types of operating systems. While this is a good practice, it can become very annoying reassigning libraries and paths that are operating system-dependent. It would be nice if there was a way to tell a Makefile to use a certain set of paths if you’re using linux, and another set if you’re using a Mac.
But there is a way! Here’s how:
One example where this comes up is when you’re compiling code that requires OpenGL. To compile in linux, you type:
-lGL -lGLU -lglut
whereas in Mac OS X you would type:
- framework GLUT -framework OpenGL -framework Carbon -L”/System/Library/Frameworks/OpenGL.framework/Libraries”
and in Windows you would type:
-lopengl32 -lglu32 -lglut32
So to tell the makefile to use either version depending on your operating system by employing the “shell uname” command:
LIBPATH = -lGL -lGLU -lglut ifeq "$(shell uname)" "Darwin" LIBPATH = -L"/System/Library/Frameworks/OpenGL.framework/Libraries" LIBPATH = -framework GLUT -framework OpenGL -framework -Carbon endif if "$(shell uname)" "Windows_NT" LIBPATH = -lopengl32 -lglu32 -lglut32 endif
We set the linux variables as default by initializing those first. Then by comparing what’s returned by “shell uname” we can alter the variables accordingly. Using this as a template should adjust your makefile accordingly.




