BushidoHacks.com - Forums - Wiki - Projects

GCC & GLUT

A straightforward guide to object-oriented programming by Bushido Hacks

About Dev-C++ and GCC for Windows

The GNU compiler collection is a free C and C++ compiler created by the Free Software Foundation that complies with ISO standards. FSF offers the GCC source codes online as part of the GNU project. FSF does not distribute binaries. Binaries are the executable programs created from the source code.

GCC comes standard with nearly every UNIX, BSD, and Linux distribution and possibly MacOS X, but not Windows. Even compiling the compiler from source code is troublesome to most Windows users. On top of that, there are a few other utilites that you need to download to take advantange of the same features on Windows that UNIX and Linux users have.

Fortunately, a few people took it upon themselves to spend the time and energy to create a Windows port of GCC and the basic binary distributions. This port is part of an online project called MinGW. MinGW is a collection of freely available and freely distributable set of Windows specific header files and import libraries combined with GNU toolsets that allow a programmer to produce native Windows programs that do not rely on any thrid-party C runtime DLL files. This freedom allows MinGW to be able to be stored onto a thumbdrive or other removable media.

However, just like GCC, MinGW is not one piece and adds a large about of internationalization that occupies a significant amout of storage. Internationalization is the translated source codes for other languages and dialects. Say for example, your native language is Chinese instead of English. While you may still want the Chinese langage support, the Spanish, French, German, Dutch, Italian, Norwegian, Sweedish, Russian, Thai, Arabic, Japanese, and several other languages that you don't speak are adding hundreds of files that you will never use. That means, many of the files that you created when you installed the program now have to be destroyed in order to reclaim space.

A good recommendation for native language support is to delete all the internationalization files except for English and your native language.

Another problem with installing MinGW directly is that you have to download several files. Just as you would rather skip to the installation instructions than read this dialog (gotcha!), you want to cut to the chase and get to work right now. (By the way, it's OK if you don't want to read this part. You can move down to the next section if you'd like.) This process takes some time for some people, time that could be used productively writing programs.

At the time this document was written, I was in a rush.

Enter Bloodshed Software's, Dev-C++. Dev-C++ is just an IDE, or Integrated Development Environment, for C, C++, and OpenGL. Personally, I don't use the IDE. Dev-C++ is quite clumsy when it comes to using tabs and whitespace, which is noticable when opening a C++ file that was created in Dev-C++ in another program such as Vim. Because the Dev-C++ IDE handles aligning codes neatly like an inebriated person walking a straight line, I like to use Vim instead of the Dev-C++ IDE. That and Vim won't crash if something bad happens.

So why install something that has trouble working in the first place? It's not so much that we need the Dev-C++ IDE, we just want to use the GNU utilies and GCC Windows binaries that come with it.

About GLUT and OpenGL

OpenGL is a standard specification defining a cross-language cross-platform application programming interface for composing applications that produce computer graphics. (Yea, alot of of big words.) Cross-language means that OpenGL is available in more than one programming lanugage such as Java and C++ but the codes are similar enough that they are compatible with other languages. Cross-platform means that OpenGL is available for more than one operating system such as Windows or Linux and can be ported to each other with a low amount of modification. An application programming interface, or API, means that OpenGL is an interface that a library (OpenGL is short for "Open Graphics Library") provides in order to allow request for services to be made of it by other computer programs to allow data to be exchanged between them. Basically, there is alot of assembly programming involved. It's something to talk about for some other time.

Unlike Java (which uses non-OpenGL graphics), C++ does not include graphics. It was this misleading belief that convinced me to not use Borland Turbo C++, which uses graphics that Borland created, and are NOT part of the C++ standard or the OpenGL API. However, many computer science instructors use Borland's graphics abilities to teach the basics of C++ and to introduce students to computer graphics.

Imagine that the C++ standard is like a bowl of cerial. Using Borland and Visual C++ are like cerial with the marshmallows in it. While the marshmallows (the added graphics from Borland and Microsoft) may make the cerials taste more appealing and more interesting, the sugared down cerial with its artificially sweetened taste eventually rots your teeth.

OpenGL is like adding your own bananas to make the bland but cruchy cerial taste better and naturally sweat. Rather than being filled up with the sugar that comes in the box of cerial, the nutrients of the banana make the cerial taste better and better for you.

The point of this analogy is that it is better to know how something works by creating your own graphics, than to be given a set of functions you don't know anything about and be told to use them because that's how they work.

The best OpenGL API allows users to create their own graphics without being tied down by a bunch of graphics drivers and .dll files that compilers like Borland and VC++ ask you to put in your system folder. The last thing any system administrator wants people to do is add or remove stuff on to their computers without their consent. (Even if you did ask, the answer would generally be "No.") This is why there are added instructions for installing software to a removable media device such as a thumbdrive in this document. You are still able to create graphics but you don't mess with their system.

In recent years, the GCC compiler has come with OpenGL headers and libraries. The headers GL/gl.h and GL/glu.h are included. Continuing the banana analogy, think of the GL directory as the banana tree. GL/gl.h and GL/glu.h are like the bananas that are still on the tree. If you don't know how to get the bananas down from the tree, you go hungry. While some people chose to climb up the tree to get the bananas, some people have create tools to bring down the bananas. One tool that was developed is called GLUT.

GLUT (rhymes with "butt") is the OpenGL Utility Toolkit. It includes added utilities that allow you create graphics without loading you with sugar as a solution. Think if GLUT as a pointed stick that knocks down a few bananas from the tree as opposed to climbing up the tree to get the bannas. Climbing the tree is the hard way. Giving up and eating cerial with sugar is the easy but wrong way. The stick, on the other hand, is not the easy way to get the bananas, it is the SMART way.

GLUT has recently been included in some UNIX and Linux distributions. Their graphics are a product of a graphics library called FreeGLUT, an open source version of the GLUT library based on another open source graphics library that uses OpenGL called Mesa. Mesa was originally an open source alternative to OpenGL until Silicon Graphics, Inc. (SGI), the creators of OpenGL, made the source codes for OpenGL publicly available. Mesa's development made it possible for GLUT to still do things that SGI had not though of. However, FreeGLUT is not for Windows, but GLUT is.

Fortuantely, a guy named Nigel Stewart has come up with a solution. While there are many Windows distros of GLUT available, Stewart's GLUT 3.7.6+ is the best. His is the only version that does not require a .dll file to be added to the system folder. (Generally, if you are told to put something in your computer that you don't know about, it is probably a good idea not to use it.) The only problem: the files are bundled in a .DevPak file, which Dev-C++'s Packman program recognizes. Hence, another reason why Dev-C++ was installed instead of MinGW.

Installation Instructions (Windows Users Only)

  1. Visit the website (optional) - Go to http://www.sourceforge.net/projects/dev-cpp/.
  2. Pick a mirror - Go to http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe?download to pick a mirror to download the installation program devcpp-4.9.9.2_setup.exe. Though I'm from St. Louis, Missouri, I pick the one from the University of Minnesota since it is the closest. Go! Goldend Gophers! :-)
  3. Download the installer - Save the file to a disk by clicking OK. The install program takes about 9 MB. The program takes about 57 MB.
  4. Install the program - Open devcpp-4.9.9.2_setup.exe
    1. Click OK
    2. Select the Language you wish to use. English is default. Click OK.
    3. Scroll through the Licensing Agreement and click I Agree.
    4. Select the following componenets with [x] and deselect the components marked [ ]. If you need to use one of the optional deselected components, you may select that option if you want.
      [x]Dev-C++ program files (required)[Required]
      [x]Example files[Optional but recommended]
      [x]Help files[Optional but recommended]
      [x]Icon files[Optional but recommended]
      [x]Mingw compiler system (binaries, headers, libraries)[Required]
      [ ]Language files[Optional/not recommneded for thumbdrive/recommended for non-English native language support]
      [ ]Associate C and C++ files to Dev-C++[Optional/not recommneded for thumbdrive]
      [ ]Create shortcuts in Start Menu[Optional/not recommneded for thumbdrive]
      [ ]Create Quicklaunch shortcut[Optional/not recommneded for thumbdrive]
      [ ]Debug files[Optional/not recommneded for thumbdrive]
      [ ]Remove all previous configuration files[Optional]
      Then click Next.
    5. By default, Dev-C++ installs in C:\Dev-Cpp. If you are installing it to a thumb drive, install it on F:\Dev-Cpp. When you have decided where to put Dev-Cpp, click Install.
    6. When installation is complete, check the box that says "Run Dev-C++ 5 beta 9 release (4.9.9.2)" if it is not already filled. Click finish. REMEMBER! We are not using Dev-C++, only the MinGW binaries that come with it.
  5. If the Tip of the day shows up, click the "Don't display on start up button" then click close. It doesn't really matter, considering you won't be using the Dev-C++ IDE, especially if you installed Dev-C++ to a thumb drive. Dev-C++ doesn't work very well on a thumbdrive, but Vim and the MinGW utilies work flawlessly.
  6. You're not done yet! (optional but recommended) - If you want to create OpenGL programs using GLUT that create graphics with C++ code, leave Dev-C++ open and continue reading this document. For those of you in a rush, you may skim ahead to the next set of instructions. If you're not interested in creating OpenGL programs, then your journey ends here.

Installing GLUT (Windows Users Only)

Remember back in the previous section when I suggessted leaving Dev-C++ open temporarly? We'll there's a good reason.
  1. Visit the website (Optional) - Goto http://www.nigels.com/glt/devpak/.
  2. Download the package - Download http://www.nigels.com/glt/devpak/glut.3.7.6+.DevPak
  3. Install the package
    1. In Dev-C++ go to the Tools menu and select Package Manager.
    2. In PakMan, go to the Package menu and select Install Package.
    3. Find glut.3.7.6+.DevPak and click Open
    4. Click Install.
    5. Click Finish.
    6. Close Packman and Dev-C++

That is all for the installation. You now have the tools to create C++ programs with computer graphics.

Using GCC

GCC is a set of programs. But the program used to compile C codes is NOT the same as the program used to compiles C++ codes. Though this document you are reading is aimed primarily for C++ programmers, the following table shows how to compile and link and a few other important instructions for C, C++, and OpenGL programmers.

Command Windows UNIX, Linux, etc.
Compiling and Linking C C:\Dev-Cpp\bin\gcc.exe file.c -o program.exe gcc file.c -o program
Compiling and Linking C++ C:\Dev-Cpp\bin\g++.exe file.cpp -o program.exe g++ file.cpp -o program
Compiling C to a Module C:\Dev-Cpp\bin\gcc.exe -c file.c -o file.o gcc -c file.c -o file.o
Compiling C++ to a Module C:\Dev-Cpp\bin\g++.exe -c file.cpp -o file.o g++ -c file.cpp -o file.o
Add or Update files in a new or existing archive C:\Dev-Cpp\bin\ar.exe cruv archive.a files.o ... ar cruv archive.a files.o ...
Link C Modules into a program C:\Dev-Cpp\bin\gcc.exe -Wl--start-group files.o ... -Wl--end-group program.exe gcc -Wl--start-group files.o ... -Wl--end-group -o program
Link C++ Modules into a program C:\Dev-Cpp\bin\g++.exe -Wl--start-group files.o ... -Wl--end-group program.exe g++ -Wl--start-group files.o ... -Wl--end-group -o program
Link C Modules into a program using archives C:\Dev-Cpp\bin\gcc.exe -Wl--start-group files.o ... archives.a ... -Wl--end-group program.exe gcc -Wl--start-group files.o ... archives.a ... -Wl--end-group -o program
Link C++ Modules into a program using archives C:\Dev-Cpp\bin\g++.exe -Wl--start-group files.o ... archives.a ... -Wl--end-group program.exe g++ -Wl--start-group files.o ... archives.a ... -Wl--end-group -o program
Link C Modules into a program for OpenGL C:\Dev-Cpp\bin\gcc.exe -Wl--start-group files.o ... -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -Wl--end-group program.exe gcc -Wl--start-group files.o ... -lglut -Wl--end-group -o program
Link C++ Modules into a program for OpenGL C:\Dev-Cpp\bin\g++.exe -Wl--start-group files.o ... -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -Wl--end-group program.exe g++ -Wl--start-group files.o ... -lglut -Wl--end-group -o program
Link C Modules into a program using archives for OpenGL C:\Dev-Cpp\bin\gcc.exe -Wl--start-group files.o ... archives.a ... -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -Wl--end-group program.exe gcc -Wl--start-group files.o ... archives.a ... -lglut -Wl--end-group -o program
Link C++ Modules into a program using archives for OpenGL C:\Dev-Cpp\bin\g++.exe -Wl--start-group files.o ... archives.a ... -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -Wl--end-group program.exe g++ -Wl--start-group files.o ... archives.a ... -lglut -Wl--end-group -o program

Creating Libraries

Here's something that is important if you have many files: creating a library or archive. Archives (.a files) are collections of modules (.o files). They are sometimes called libraries especially since some archives end with .lib.

Archives are important if you use many modules. To create an archive, use F:\Dev-Cpp\bin\ar.exe. ar.exe is the Windows port of the UNIX and Linux program ar (pronounced "ARRRRR!" like a pirate). ar is the predcessor to the tar program, used on UNIX and Linux that creates archives, which are then compressed with compression programs such as gzip or bzip. Unlike ar, tar cannot be used to bundle modules.

You should create an archive if there is a list of modules that you are happy with. A copy of the modules are put into the archive. Archives are used during linking. If you compare the linking procedure using libraries inseated of a long list of modules, you will see that using a library is much more efficent.

Batch files (Windows Users only)


Download a copy of this batch file: c++.bat
Download a copy of this batch file: ar.bat
Download a copy of this batch file: link.bat
Download a copy of this batch file: glink.bat

FAQ

Q.Why are the executable files so big?
A.That's a question many GCC users and programmers have yet to figure out. What the GCC programmers do know is that it has something to do with the Windows Operating System.
Q.Why don't you use Borland or Microsoft?
A.For various reasons.
  1. Neither of these two commerical compilers follow ISO standards.
  2. Borland's use of void main() is one of the things I really don't like and is a bad practice to use in the real world.
  3. Microsoft adds a copyright to your programs claiming it as their own. Not only is that greedy and selfrightous of Microsoft to add code that claims what you made as their own. Microsoft Virtual C++ also uses a form of exeception handling when using pointers for creating dynamic arrays. They add keywords like (nothrow) which is very un-C++ like and obfuscates the use of pointers and exception handling. Therefore VC++ is NOT C++.
  4. On websites, IRC chat rooms, and in USENET groups like comp.lang.c++ and comp.graphics.api.opengl, most programmers use the ISO dialect of C++.
  5. Under the No Electronic Theft (NET) Act of 1997 (H.R. 2265), it is illegal to make copies of commerical software available for download online even if it is for non-commerical or educational purposes.
Q.Why didn't you create shell scripts for UNIX or Linux users?
A.If you noticed the table listing the differences between Windows and Linux commands, the Windows Commands are longer than the Linux commands.
Q.Why the batch files only handle one file at a time?
A.In the past, I created batch files that handled more than one file at a time. The programming got messy and time consuming. It is more organized to handle two files than it is a dozen. There is a joke that computer scientists use often to describe handling large tasks. "How do you eat an elephant? One bite at a time." It holds true considering the best way to handle a large project is to break it down into parts.

Links and resources

There are a few links and resources you should bookmark that were not listed that are very important and quite resourceful.
© 2006 BushidoHacks.com 1508 visitors