Linux saves my day again

Published June 30, 2011
Advertisement
Why hello there GDNet. Once again the odd ball me gets to share something that not may GDNet people get to experience all that often. The topic of today has to do with how Linux has saved the day for me. I am sure many people here already know I am a very avid Linux user. I don't have anything against Windows I have that too after all. I like to play AAA games and to do that effectively I just dual boot. Despite this I still do a majority of my work under Linux. This is because I find it very very productive. I find the POSIX interface to be a life saver in many circumstances like the one I will explain today.

First as you may know if you read my blog I am in the process of learning OpenGL. This is a huge step for me because I have been working with 2D for way too long. I fee this is the next logical step for me in interest. To do this I am using the OpenGL Superbible 5th edition which covers the GL 3 core profile. During the book the author eases you into OpenGL by introducing concepts through the library they developed called GLTools. As you progress through the book they start to strip away GLTools so they can introduce you into each concept a little at a time.

Now the problem. I need to set up this library on Linux. The first issue was getting the code. This was mentioned in my last blurb on the blog I was using git-svn to pull down the svn repo. This took forever about 20 min or so. For such a small amount of code this was shocking. I realized later it was actually capitolized on because even tho SVN is slow git had to rebuild the entire repo. Oh Well task one done.

Now task two I need to build the GLTools library and the Glew library. So I navigate into the repo and stop dead wait a second there is no Makefile. So I shoot back to the Linux directory and look there wait no Makefile. They had Makefiles for every project but none for GLTools/Glew then I saw it. They were building GLTools and Glew for every project and storing the libs local to the project. EWE. So now I need to write a new Makefile for this stuff.

Step 3 ok so I fire up Emacs and hack up a Makefile. Once it is done I type make all and it starts and KABOOM. Cannot find header glew.h WTF. So at this point nothing built because GLTools uses Glew to fire up the Extensions required for the OGL 3 core profile. So I navigate up and see that the glew. h file is present so I go and look at my make file and see if I made a mistake. I did not. Turns out in the ifdef preprocessor for Linux they are including instead of which is where they had the file stored. So I moved the header and tried again. KABOOM can't find glew.h. Turns out Glew looks for glew.h in the GL directory. Oh Bugger. Now how are we going to fix this? Before we get into that here is the Makefile if anyone else actually needs to go through this.

[source]
# Compiles the OpenGL SuperBible 5th Edition GLTools Library including
# the Glew library.

# Below are project specific variables to setup the proper directories
# to pass to the compiler and linker.
GLTOOLS = libGLTools
GLEW = libglew
SRCPATH = ./src/
INCPATH = -I./include

# Below are variables for the compiler, linker, and also the flags
# pertaining to the compiler and linker.
CXX = g++
CXXFLAGS = $(INCPATH)
AR = ar
ARFLAGS = -rcs

# The actual compilation and linking process goes on down here.

# Compile and link everything
all : $(GLTOOLS) $(GLEW) $(GLTOOLS).a $(GLEW).a

# Basic setup of object file dependencies
GLBatch.o : $(SRCPATH)GLBatch.cpp
GLShaderManager.o : $(SRCPATH)GLShaderManager.cpp
GLTriangleBatch.o : $(SRCPATH)GLTriangleBatch.cpp
GLTools.o : $(SRCPATH)GLTools.cpp
math3d.o : $(SRCPATH)math3d.cpp
glew.o : $(SRCPATH)glew.c

# Compile GLTools
$(GLTOOLS) :
$(CXX) $(CXXFLAGS) -c $(SRCPATH)*.cpp

# Archive GLTools
$(GLTOOLS).a : GLBatch.o GLShaderManager.o GLTriangleBatch.o GLTools.o math3d.o
$(AR) $(ARFLAGS) $(GLTOOLS).a *.o

# Compile Glew
$(GLEW) :
$(CXX) $(CXXFLAGS) -c $(SRCPATH)glew.c

# Archive Glew
$(GLEW).a : glew.o
$(AR) $(ARFLAGS) $(GLEW).a glew.o

# Cleanup
clean :
rm -rf *.o
[/source]

Ok now that this is out of the way how do we fix it. Well POSIX + Linux to the rescue. So here is the problem. We have a directory of 11 header files. We do not know which header files contain the declaration for glew.h because the make file is bailing on us before it tries the others due to dependencies needed to continue the build. We don't want to open all 11 files into an editor and manually change all of those. For one we are programmers and programmers are lazy. This is a total waste of time so lets use the power of our POSIX based command line BOOYAH. So here is what we need to do. We need to first find all the header files then we need to search each header files for and replace it with . I know you are asking how are you going to do that? Well let me explain. On POSIX based systems each command you use at the terminal has 3 different streams stdin, stdout, and stderr. The nice thing is since every command has a proper in, out and err we can actually by definition in the POSIX standard "Pipe" together different commands to transfur the data onto another process. So to do this task there are 2 commands we need. The first is find which basically reads the specified directory structure and outputs a list of that structure. Then we need a command called sed which is actually a data stream manipulation command. It basically allows you to hack and modify the data streams to bits. So what we need to do is find the headers and modify them with sed so that we can make the correction in one swoop without needing to open all the files and type the fixes by hand. Here is how this is done.




find . \( -name GL -prune \) , \( -name '*.h' -type f \) -exec sed -i 's/\(<\)\(\bglew\.h\)\(<\)//g' '{}' +




Basically what is going on here is we are telling find we want all of the header files in the current directory structure minus the directory structure of GL and pipe it into sed to use a regex search to find and change it to for every file in the list that find provides.

Cool stuff one line fixes all the the appropriate files and boom make all compiles everything I need. Go Go POSIX and Linux.
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement